?
This document uses PHP Chinese website manual Release
import "net/smtp"
概觀
索引
示例
smtp 包實(shí)現(xiàn)了RFC 5321中定義的簡(jiǎn)單郵件傳輸協(xié)議。它還實(shí)現(xiàn)了以下擴(kuò)展:
8BITMIME RFC 1652AUTH RFC 2554STARTTLS RFC 3207
其他擴(kuò)展可能由客戶端處理。
smtp 軟件包被凍結(jié),不接受新的功能。一些外部軟件包提供更多功能??吹剑?/p>
https://godoc.org/?q=smtp
編碼:
// Connect to the remote SMTP server.c, err := smtp.Dial("mail.example.com:25")if err != nil { log.Fatal(err)}// Set the sender and recipient firstif err := c.Mail("sender@example.org"); err != nil { log.Fatal(err)}if err := c.Rcpt("recipient@example.net"); err != nil { log.Fatal(err)}// Send the email body.wc, err := c.Data()if err != nil { log.Fatal(err)}_, err = fmt.Fprintf(wc, "This is the email body")if err != nil { log.Fatal(err)}err = wc.Close()if err != nil { log.Fatal(err)}// Send the QUIT command and close the connection.err = c.Quit()if err != nil { log.Fatal(err)}
func SendMail(addr string, a Auth, from string, to []string, msg []byte) error
type Auth
func CRAMMD5Auth(username, secret string) Auth
func PlainAuth(identity, username, password, host string) Auth
type Client
func Dial(addr string) (*Client, error)
func NewClient(conn net.Conn, host string) (*Client, error)
func (c *Client) Auth(a Auth) error
func (c *Client) Close() error
func (c *Client) Data() (io.WriteCloser, error)
func (c *Client) Extension(ext string) (bool, string)
func (c *Client) Hello(localName string) error
func (c *Client) Mail(from string) error
func (c *Client) Quit() error
func (c *Client) Rcpt(to string) error
func (c *Client) Reset() error
func (c *Client) StartTLS(config *tls.Config) error
func (c *Client) TLSConnectionState() (state tls.ConnectionState, ok bool)
func (c *Client) Verify(addr string) error
type ServerInfo
打包 PlainAuth SendMail
auth.go smtp.go
func SendMail(addr string, a Auth, from string, to []string, msg []byte) error
SendMail 在 addr 連接到服務(wù)器,如果可能的話,切換到 TLS,如果可能,使用可選機(jī)制進(jìn)行身份驗(yàn)證,然后使用消息 msg 從地址發(fā)送到地址的電子郵件。addr 必須包含一個(gè)端口,如“mail.example.com:smtp”中所示。
to 參數(shù)中的地址是 SMTP RCPT 地址。
msg 參數(shù)應(yīng)該是一個(gè) RFC 822 風(fēng)格的電子郵件,首先包含標(biāo)題,空白行,然后是郵件正文。msg 的行應(yīng)該是 CRLF 終止的。 msg 標(biāo)題通常應(yīng)包含“From”,“To”,“Subject”和“Cc”等字段。發(fā)送“密件抄送”郵件是通過在 to 參數(shù)中包含一個(gè)電子郵件地址來完成的,但不包括在msg頭文件中。
SendMail 函數(shù)和 net / smtp 包是低級(jí)機(jī)制,不支持 DKIM 簽名,MIME 附件(請(qǐng)參閱mime / multipart包)或其他郵件功能。更高級(jí)別的軟件包存在于標(biāo)準(zhǔn)庫(kù)之外。
package mainimport ("log""net/smtp")func main() {// Set up authentication information. auth := smtp.PlainAuth("", "user@example.com", "password", "mail.example.com")// Connect to the server, authenticate, set the sender and recipient,// and send the email all in one step. to := []string{"recipient@example.net"} msg := []byte("To: recipient@example.net\r\n" +"Subject: discount Gophers!\r\n" +"\r\n" +"This is the email body.\r\n") err := smtp.SendMail("mail.example.com:25", auth, "sender@example.org", to, msg)if err != nil { log.Fatal(err)}}
身份驗(yàn)證通過 SMTP 身份驗(yàn)證機(jī)制實(shí)施。
type Auth interface { // Start begins an authentication with a server. // It returns the name of the authentication protocol // and optionally data to include in the initial AUTH message // sent to the server. It can return proto == "" to indicate // that the authentication should be skipped. // If it returns a non-nil error, the SMTP client aborts // the authentication attempt and closes the connection. Start(server *ServerInfo) (proto string, toServer []byte, err error) // Next continues the authentication. The server has just sent // the fromServer data. If more is true, the server expects a // response, which Next should return as toServer; otherwise // Next should return toServer == nil. // If Next returns a non-nil error, the SMTP client aborts // the authentication attempt and closes the connection. Next(fromServer []byte, more bool) (toServer []byte, err error)}
func CRAMMD5Auth(username, secret string) Auth
CRAMMD5Auth 返回一個(gè)Auth,它實(shí)現(xiàn) RFC 2195 中定義的 CRAM-MD5 認(rèn)證機(jī)制。返回的 Auth 使用給定的用戶名和密碼使用質(zhì)詢 - 響應(yīng)機(jī)制向服務(wù)器進(jìn)行認(rèn)證。
func PlainAuth(identity, username, password, host string) Auth
PlainAuth 返回一個(gè) Auth,它實(shí)現(xiàn) RFC 4616 中定義的 PLAIN 身份驗(yàn)證機(jī)制。返回的 Auth 使用給定的用戶名和密碼對(duì)主機(jī)進(jìn)行身份驗(yàn)證,并充當(dāng)身份。通常身份應(yīng)該是空字符串,充當(dāng)用戶名。
如果連接使用 TLS 或連接到本地主機(jī),PlainAuth 將僅發(fā)送憑據(jù)。否則,身份驗(yàn)證將失敗并出現(xiàn)錯(cuò)誤,而不會(huì)發(fā)送憑據(jù)。
編碼:
// hostname is used by PlainAuth to validate the TLS certificate.hostname := "mail.example.com"auth := smtp.PlainAuth("", "user@example.com", "password", hostname)err := smtp.SendMail(hostname+":25", auth, from, recipients, msg)if err != nil { log.Fatal(err)}
客戶端代表到 SMTP 服務(wù)器的客戶端連接。
type Client struct { // Text is the textproto.Conn used by the Client. It is exported to allow for // clients to add extensions. Text *textproto.Conn // contains filtered or unexported fields}
func Dial(addr string) (*Client, error)
Dial 返回一個(gè)新的連接到 SMTP 服務(wù)器的客戶端。 addr 必須包含一個(gè)端口,如“mail.example.com:smtp”中所示。
func NewClient(conn net.Conn, host string) (*Client, error)
NewClient 使用現(xiàn)有連接和主機(jī)作為服務(wù)器名稱返回一個(gè)新的客戶端,以便在進(jìn)行身份驗(yàn)證時(shí)使用。
func (c *Client) Auth(a Auth) error
Auth 使用提供的認(rèn)證機(jī)制來認(rèn)證客戶端。認(rèn)證失敗將關(guān)閉連接。只有通告 AUTH 擴(kuò)展的服務(wù)器才支持此功能。
func (c *Client) Close() error
Close 關(guān)閉連接。
func (c *Client) Data() (io.WriteCloser, error)
數(shù)據(jù)向服務(wù)器發(fā)出 DATA 命令并返回可用于寫入郵件標(biāo)題和正文的編寫器。調(diào)用者應(yīng)該在調(diào)用c上的更多方法之前關(guān)閉作者。對(duì)數(shù)據(jù)的調(diào)用必須在一個(gè)或多個(gè)對(duì) Rcpt 的調(diào)用之前進(jìn)行。
func (c *Client) Extension(ext string) (bool, string)
Extension 報(bào)告擴(kuò)展是否受服務(wù)器支持。擴(kuò)展名不區(qū)分大小寫。如果支持?jǐn)U展,Extension 還會(huì)返回一個(gè)字符串,其中包含服務(wù)器為擴(kuò)展指定的任何參數(shù)。
func (c *Client) Hello(localName string) error
Hello 將 HELO 或 EHLO 作為給定的主機(jī)名稱發(fā)送到服務(wù)器。僅當(dāng)客戶端需要控制所使用的主機(jī)名稱時(shí)才需要調(diào)用此方法。否則客戶端會(huì)自動(dòng)將自身介紹為“本地主機(jī)”。如果調(diào)用 Hello,則必須在任何其他方法之前調(diào)用它。
func (c *Client) Mail(from string) error
郵件使用提供的電子郵件地址向服務(wù)器發(fā)出MAIL 命令。如果服務(wù)器支持 8BITMIME 擴(kuò)展,則 Mail 會(huì)添加 BODY = 8BITMIME 參數(shù)。這將啟動(dòng)一個(gè)郵件事務(wù),然后是一個(gè)或多個(gè) Rcpt 調(diào)用。
func (c *Client) Quit() error
退出發(fā)送 QUIT 命令并關(guān)閉連接到服務(wù)器。
func (c *Client) Rcpt(to string) error
Rcpt 使用提供的電子郵件地址向服務(wù)器發(fā)出 RCPT 命令。對(duì) Rcpt 的調(diào)用必須在對(duì) Mail 的調(diào)用之后進(jìn)行,然后可以進(jìn)行數(shù)據(jù)調(diào)用或其他 Rcpt 調(diào)用。
func (c *Client) Reset() error
重置將 RSET 命令發(fā)送到服務(wù)器,中止當(dāng)前的郵件事務(wù)。
func (c *Client) StartTLS(config *tls.Config) error
StartTLS 發(fā)送 STARTTLS 命令并加密所有進(jìn)一步的通信。只有通告 STARTTLS 擴(kuò)展的服務(wù)器才支持此功能。
func (c *Client) TLSConnectionState() (state tls.ConnectionState, ok bool)
TLSConnectionState 返回客戶端的 TLS 連接狀態(tài)。如果 StartTLS 不成功,則返回值為零值。
func (c *Client) Verify(addr string) error
驗(yàn)證檢查服務(wù)器上電子郵件地址的有效性。如果驗(yàn)證返回 nil ,則地址有效。非零返回不一定表示無效地址。出于安全原因,許多服務(wù)器不會(huì)驗(yàn)證地址。
ServerInfo 記錄有關(guān) SMTP 服務(wù)器的信息。
type ServerInfo struct { Name string // SMTP server name TLS bool // using TLS, with valid certificate for Name Auth []string // advertised authentication mechanisms}