// SendMailTLS not use STARTTLS commond funcSendMailTLS(addr string, auth smtp.Auth, from string, to []string, msg []byte)error { host, _, err := net.SplitHostPort(addr) if err != nil { return err } tlsconfig := &tls.Config{ServerName: host} if err = validateLine(from); err != nil { return err } for _, recp := range to { if err = validateLine(recp); err != nil { return err } } conn, err := tls.Dial("tcp", addr, tlsconfig) if err != nil { return err } defer conn.Close() c, err := smtp.NewClient(conn, host) if err != nil { return err } defer c.Close() if err = c.Hello("localhost"); err != nil { return err } if err = c.Auth(auth); err != nil { return err } if err = c.Mail(from); err != nil { return err } for _, addr := range to { if err = c.Rcpt(addr); err != nil { return err } } w, err := c.Data() if err != nil { return err } _, err = w.Write(msg) if err != nil { return err } err = w.Close() if err != nil { return err } return c.Quit() }
// validateLine checks to see if a line has CR or LF as per RFC 5321 funcvalidateLine(line string)error { if strings.ContainsAny(line, "\n\r") { return errors.New("a line must not contain CR or LF") } returnnil }
MIME
多用途互联网邮件扩展(MIME,Multipurpose Internet Mail Extensions)是一个互联网标准,它扩展了电子邮件标准,使其能够支援: 非ASCII字符文本; 非文本格式附件(二进制、声音、图像等); 由多部分(multiple parts)组成的消息体; 包含非ASCII字符的头信息(Header information)。这个标准被定义在RFC 2045、RFC 2046、RFC 2047、RFC 2048、RFC 2049等RFC中。 MIME改善了由RFC 822转变而来的RFC 2822,这些旧标准规定电子邮件标准并不允许在邮件消息中使用7位ASCII字符集以外的字符。正因如此,一些非英语字符消息和二进制文件,图像,声音等非文字消息原本都不能在电子邮件中传输(MIME可以)。MIME规定了用于表示各种各样的数据类型的符号化方法。此外,在万维网中使用的HTTP协议中也使用了MIME的框架,标准被扩展为互联网媒体类型。
S: 220 xyz.com Simple Mail Transfer Service Ready C: EHLO foo.com S: 250 xyz.com is on the air C: MAIL FROM:<@foo.com:[email protected]> S: 250 OK C: RCPT TO:<[email protected]> S: 250 OK C: DATA S: 354 Start mail input; end with <CRLF>.<CRLF> C: Received: from bar.com by foo.com ; Thu, 21 May 1998 C: 05:33:29 -0700 C: Date: Thu, 21 May 1998 05:33:22 -0700 C: From: John Q. Public <[email protected]> C: Subject: The Next Meeting of the Board C: To: [email protected] C: C: Bill: C: The next meeting of the board of directors will be C: on Tuesday. C: John. C: . S: 250 OK C: QUIT S: 221 foo.com Service closing transmission channel