?
本文檔使用 php中文網(wǎng)手冊 發(fā)布
import "net/smtp"
概觀
索引
示例
smtp 包實現(xiàn)了RFC 5321中定義的簡單郵件傳輸協(xié)議。它還實現(xiàn)了以下擴展:
8BITMIME RFC 1652AUTH RFC 2554STARTTLS RFC 3207
其他擴展可能由客戶端處理。
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,如果可能,使用可選機制進行身份驗證,然后使用消息 msg 從地址發(fā)送到地址的電子郵件。addr 必須包含一個端口,如“mail.example.com:smtp”中所示。
to 參數(shù)中的地址是 SMTP RCPT 地址。
msg 參數(shù)應(yīng)該是一個 RFC 822 風格的電子郵件,首先包含標題,空白行,然后是郵件正文。msg 的行應(yīng)該是 CRLF 終止的。 msg 標題通常應(yīng)包含“From”,“To”,“Subject”和“Cc”等字段。發(fā)送“密件抄送”郵件是通過在 to 參數(shù)中包含一個電子郵件地址來完成的,但不包括在msg頭文件中。
SendMail 函數(shù)和 net / smtp 包是低級機制,不支持 DKIM 簽名,MIME 附件(請參閱mime / multipart包)或其他郵件功能。更高級別的軟件包存在于標準庫之外。
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)}}
身份驗證通過 SMTP 身份驗證機制實施。
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 返回一個Auth,它實現(xiàn) RFC 2195 中定義的 CRAM-MD5 認證機制。返回的 Auth 使用給定的用戶名和密碼使用質(zhì)詢 - 響應(yīng)機制向服務(wù)器進行認證。
func PlainAuth(identity, username, password, host string) Auth
PlainAuth 返回一個 Auth,它實現(xiàn) RFC 4616 中定義的 PLAIN 身份驗證機制。返回的 Auth 使用給定的用戶名和密碼對主機進行身份驗證,并充當身份。通常身份應(yīng)該是空字符串,充當用戶名。
如果連接使用 TLS 或連接到本地主機,PlainAuth 將僅發(fā)送憑據(jù)。否則,身份驗證將失敗并出現(xiàn)錯誤,而不會發(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 返回一個新的連接到 SMTP 服務(wù)器的客戶端。 addr 必須包含一個端口,如“mail.example.com:smtp”中所示。
func NewClient(conn net.Conn, host string) (*Client, error)
NewClient 使用現(xiàn)有連接和主機作為服務(wù)器名稱返回一個新的客戶端,以便在進行身份驗證時使用。
func (c *Client) Auth(a Auth) error
Auth 使用提供的認證機制來認證客戶端。認證失敗將關(guān)閉連接。只有通告 AUTH 擴展的服務(wù)器才支持此功能。
func (c *Client) Close() error
Close 關(guān)閉連接。
func (c *Client) Data() (io.WriteCloser, error)
數(shù)據(jù)向服務(wù)器發(fā)出 DATA 命令并返回可用于寫入郵件標題和正文的編寫器。調(diào)用者應(yīng)該在調(diào)用c上的更多方法之前關(guān)閉作者。對數(shù)據(jù)的調(diào)用必須在一個或多個對 Rcpt 的調(diào)用之前進行。
func (c *Client) Extension(ext string) (bool, string)
Extension 報告擴展是否受服務(wù)器支持。擴展名不區(qū)分大小寫。如果支持擴展,Extension 還會返回一個字符串,其中包含服務(wù)器為擴展指定的任何參數(shù)。
func (c *Client) Hello(localName string) error
Hello 將 HELO 或 EHLO 作為給定的主機名稱發(fā)送到服務(wù)器。僅當客戶端需要控制所使用的主機名稱時才需要調(diào)用此方法。否則客戶端會自動將自身介紹為“本地主機”。如果調(diào)用 Hello,則必須在任何其他方法之前調(diào)用它。
func (c *Client) Mail(from string) error
郵件使用提供的電子郵件地址向服務(wù)器發(fā)出MAIL 命令。如果服務(wù)器支持 8BITMIME 擴展,則 Mail 會添加 BODY = 8BITMIME 參數(shù)。這將啟動一個郵件事務(wù),然后是一個或多個 Rcpt 調(diào)用。
func (c *Client) Quit() error
退出發(fā)送 QUIT 命令并關(guān)閉連接到服務(wù)器。
func (c *Client) Rcpt(to string) error
Rcpt 使用提供的電子郵件地址向服務(wù)器發(fā)出 RCPT 命令。對 Rcpt 的調(diào)用必須在對 Mail 的調(diào)用之后進行,然后可以進行數(shù)據(jù)調(diào)用或其他 Rcpt 調(diào)用。
func (c *Client) Reset() error
重置將 RSET 命令發(fā)送到服務(wù)器,中止當前的郵件事務(wù)。
func (c *Client) StartTLS(config *tls.Config) error
StartTLS 發(fā)送 STARTTLS 命令并加密所有進一步的通信。只有通告 STARTTLS 擴展的服務(wù)器才支持此功能。
func (c *Client) TLSConnectionState() (state tls.ConnectionState, ok bool)
TLSConnectionState 返回客戶端的 TLS 連接狀態(tài)。如果 StartTLS 不成功,則返回值為零值。
func (c *Client) Verify(addr string) error
驗證檢查服務(wù)器上電子郵件地址的有效性。如果驗證返回 nil ,則地址有效。非零返回不一定表示無效地址。出于安全原因,許多服務(wù)器不會驗證地址。
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}