Example #1
0
func TestImap(t *testing.T) {
	var user, pw string
	if mock {
		testDial = fakeDial
		user = "******"
		pw = "password"
	} else {
		acct := google.Acct("*****@*****.**")
		user = acct.Email
		pw = acct.Password
	}
	c, err := NewClient(TLS, "imap.gmail.com", user, pw, "")
	if err != nil {
		t.Fatal(err)
	}

	inbox := c.Inbox()
	msgs := inbox.Msgs()

	for _, m := range msgs {
		if m.UID == 611764547<<32|57046 {
			//			c.io.lock()
			//			c.cmd(c.boxByName[`[Gmail]/All Mail`], `UID SEARCH X-GM-RAW "label:[email protected] in:inbox in:unread -in:muted"`)
			//			c.cmd(c.inbox, `UID SEARCH X-GM-RAW "label:[email protected] in:inbox in:unread -in:muted"`)
			//			c.cmd(c.boxByName[`To Read`], `UID SEARCH X-GM-RAW "label:[email protected] in:inbox in:unread -in:muted"`)
			//			c.cmd(c.boxByName[`[Gmail]/All Mail`], `UID SEARCH X-GM-RAW "label:[email protected] in:inbox in:unread -in:muted"`)
			//			c.fetch(m.Root.Child[0], "")
			//			c.io.unlock()
			fmt.Println("--")
			fmt.Println("From:", m.Hdr.From)
			fmt.Println("To:", m.Hdr.To)
			fmt.Println("Subject:", m.Hdr.Subject)
			fmt.Println("M-Date:", m.Date)
			fmt.Println("Date:", m.Hdr.Date)
			fmt.Println()
			fmt.Println(string(m.Root.Child[0].Text()))
			fmt.Println("--")
		}
	}
	c.Close()
}
Example #2
0
func main() {
	flag.StringVar(&inReplyTo, "in-reply-to", "", "In-Reply-To")
	flag.StringVar(&subject, "s", "", "Subject")
	flag.Var(&from, "from", "From (can repeat)")
	flag.Var(&to, "to", "To (can repeat)")
	flag.Var(&cc, "cc", "CC (can repeat)")
	flag.Var(&bcc, "bcc", "BCC (can repeat)")
	flag.Var(&replyTo, "replyTo", "Reply-To (can repeat)")

	flag.Parse()
	if flag.NArg() != 0 && !*inputHeader {
		flag.Usage()
	}

	var body bytes.Buffer
	input := bufio.NewReader(os.Stdin)
	if *inputHeader {
		holdmode()
	Loop:
		for {
			s, err := input.ReadString('\n')
			if err != nil {
				if err == io.EOF {
					break Loop
				}
				fmt.Fprintf(os.Stderr, "reading stdin: %s\n", err)
				os.Exit(2)
			}
			var arg string
			switch {
			default:
				if ok, _ := regexp.MatchString(`^\S+:`, s); ok {
					fmt.Fprintf(os.Stderr, "unknown header line: %s", s)
					os.Exit(2)
				}
				body.WriteString(s)
				break Loop
			case match(s, "from:", &arg):
				from.parseLine(arg)
			case match(s, "to:", &arg):
				to.parseLine(arg)
			case match(s, "cc:", &arg):
				cc.parseLine(arg)
			case match(s, "bcc:", &arg):
				bcc.parseLine(arg)
			case match(s, "reply-to:", &arg):
				replyTo.parseLine(arg)
			case match(s, "subject:", &arg):
				subject = arg
			case match(s, "in-reply-to:", &arg):
				inReplyTo = arg
			}
		}
	}

	acct = google.Acct(*acctName)
	from.fixDomain()
	to.fixDomain()
	cc.fixDomain()
	bcc.fixDomain()
	replyTo.fixDomain()

	smtpTo := append(append(to, cc...), bcc...)

	if len(from) == 0 {
		// TODO: Much better
		name := ""
		email := acct.Email
		if email == "*****@*****.**" || email == "*****@*****.**" {
			name = "Russ Cox"
		}
		if email == "*****@*****.**" && (smtpTo.has("*****@*****.**") || smtpTo.has("*****@*****.**") || smtpTo.has("*****@*****.**")) {
			from = append(from, Addr{name, "*****@*****.**"})
		} else {
			from = append(from, Addr{name, email})
		}
	}

	if len(from) > 1 {
		fmt.Fprintf(os.Stderr, "missing -from\n")
		os.Exit(2)
	}

	if len(to)+len(cc)+len(bcc) == 0 {
		fmt.Fprintf(os.Stderr, "missing destinations\n")
		os.Exit(2)
	}

	if !*inputHeader {
		holdmode()
	}
	_, err := io.Copy(&body, input)
	if err != nil {
		fmt.Fprintf(os.Stderr, "reading stdin: %s\n", err)
		os.Exit(2)
	}

	if *appendFile != "" {
		f, err := os.Open(*appendFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "append: %s\n", err)
			os.Exit(2)
		}
		_, err = io.Copy(&body, f)
		f.Close()
		if err != nil {
			fmt.Fprintf(os.Stderr, "append: %s\n", err)
			os.Exit(2)
		}
	}

	var msg bytes.Buffer
	fmt.Fprintf(&msg, "MIME-Version: 1.0\n")
	if len(from) > 0 {
		fmt.Fprintf(&msg, "From: ")
		for i, a := range from {
			if i > 0 {
				fmt.Fprintf(&msg, ", ")
			}
			fmt.Fprintf(&msg, "%s", a.enc())
		}
		fmt.Fprintf(&msg, "\n")
	}
	if len(to) > 0 {
		fmt.Fprintf(&msg, "To: ")
		for i, a := range to {
			if i > 0 {
				fmt.Fprintf(&msg, ", ")
			}
			fmt.Fprintf(&msg, "%s", a.enc())
		}
		fmt.Fprintf(&msg, "\n")
	}
	if len(cc) > 0 {
		fmt.Fprintf(&msg, "CC: ")
		for i, a := range cc {
			if i > 0 {
				fmt.Fprintf(&msg, ", ")
			}
			fmt.Fprintf(&msg, "%s", a.enc())
		}
		fmt.Fprintf(&msg, "\n")
	}
	if len(replyTo) > 0 {
		fmt.Fprintf(&msg, "Reply-To: ")
		for i, a := range replyTo {
			if i > 0 {
				fmt.Fprintf(&msg, ", ")
			}
			fmt.Fprintf(&msg, "%s", a.enc())
		}
		fmt.Fprintf(&msg, "\n")
	}
	if inReplyTo != "" {
		fmt.Fprintf(&msg, "In-Reply-To: %s\n", inReplyTo)
	}
	if subject != "" {
		fmt.Fprintf(&msg, "Subject: %s\n", enc(subject))
	}
	fmt.Fprintf(&msg, "Date: xxx\n")
	fmt.Fprintf(&msg, "Content-Type: text/plain; charset=\"utf-8\"\n")
	fmt.Fprintf(&msg, "Content-Transfer-Encoding: base64\n")
	fmt.Fprintf(&msg, "\n")
	enc64 := base64.StdEncoding.EncodeToString(body.Bytes())
	for len(enc64) > 72 {
		fmt.Fprintf(&msg, "%s\n", enc64[:72])
		enc64 = enc64[72:]
	}
	fmt.Fprintf(&msg, "%s\n\n", enc64)

	auth := smtp.PlainAuth(
		"",
		acct.Email,
		acct.Password,
		"smtp.gmail.com",
	)
	var smtpToEmail []string
	for _, a := range smtpTo {
		if a.Email != "" {
			smtpToEmail = append(smtpToEmail, a.Email)
		}
	}

	if err := sendMail("smtp.gmail.com:587", auth, from[0].Email, smtpToEmail, msg.Bytes()); err != nil {
		fmt.Fprintf(os.Stderr, "sending mail: %s\n", err)
		os.Exit(2)
	}
}