Documentation
¶
Overview ¶
smtp パッケージは RFC 5321 で定義されている Simple Mail Transfer Protocol を実装しています。 さらに、以下の拡張も実装しています:
8BITMIME RFC 1652 AUTH RFC 2554 STARTTLS RFC 3207
クライアント側で追加の拡張も扱うことができます。
smtp パッケージは凍結されており、新しい機能の追加は受け付けていません。 いくつかの外部パッケージがより多機能を提供しています。以下を参照してください:
https://godoc.org/?q=smtp
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/log" "github.com/shogo82148/std/net/smtp" ) func main() { // リモートSMTPサーバーに接続する。 c, err := smtp.Dial("mail.example.com:25") if err != nil { log.Fatal(err) } // まず、送信者と受信者を設定します if err := c.Mail("sender@example.org"); err != nil { log.Fatal(err) } if err := c.Rcpt("recipient@example.net"); err != nil { log.Fatal(err) } // メール本文を送信する。 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) } // QUITコマンドを送信し、接続を閉じます。 err = c.Quit() if err != nil { log.Fatal(err) } }
Output:
Index ¶
- func SendMail(addr string, a Auth, from string, to []string, msg []byte) error
- type Auth
- type Client
- 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) Noop() 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
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SendMail ¶
SendMailはaddrで指定されたサーバに接続し、可能な場合はTLSに切り替え、必要に応じてオプションのメカニズムaで認証し、fromからのアドレス、toへのアドレス、メッセージmsgを送信します。 addrにはポートを含める必要があります。例:"mail.example.com:smtp"
toパラメータのアドレスは、SMTPのRCPTアドレスです。
msgパラメータは、ヘッダ、空行、メッセージ本文の順になったRFC 822スタイルの電子メールである必要があります。msgの各行はCRLFで終端する必要があります。msgのヘッダには通常、"From"、"To"、"Subject"、"Cc"などのフィールドが含まれるべきです。"Bcc"メッセージを送信するには、toパラメータにメールアドレスを含め、msgのヘッダには含めません。
SendMail関数とnet/smtpパッケージは低レベルのメカニズムであり、DKIM署名、MIME添付ファイル(mime/multipartパッケージを参照)、その他のメール機能をサポートしていません。高レベルのパッケージは標準ライブラリの外部に存在します。
Example ¶
package main import ( "github.com/shogo82148/std/log" "github.com/shogo82148/std/net/smtp" ) func main() { // 認証情報を設定する。 auth := smtp.PlainAuth("", "user@example.com", "password", "mail.example.com") // サーバーに接続し、認証し、送信元と受信者を設定し、 // メールを一括で送信します。 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) } }
Output:
Types ¶
type Auth ¶
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. // 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) }
AuthはSMTP認証メカニズムによって実装されます。
func CRAMMD5Auth ¶
CRAMMD5AuthはRFC 2195で定義されたCRAM-MD5認証メカニズムを実装するAuthを返します。 返されたAuthは、ユーザー名と秘密情報を使用して、チャレンジ-レスポンスメカニズムを使ってサーバーに認証します。
func PlainAuth ¶
PlainAuthは、RFC 4616で定義されているPLAIN認証メカニズムを実装するAuthを返します。返されたAuthは、指定したユーザー名とパスワードを使用してホストに認証し、アイデンティティとして動作します。通常、アイデンティティは空の文字列であるべきです。これにより、ユーザー名として機能します。 PlainAuthは、接続がTLSを使用しているか、またはlocalhostに接続されている場合にのみ認証情報を送信します。それ以外の場合は、認証に失敗し、認証情報は送信されません。
Example ¶
// hostnameはPlainAuthによってTLS証明書の検証に使用されます。 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) }
Output:
type Client ¶
type Client struct { // TextはClientによって使用されるtextproto.Connです。拡張機能を追加できるように、公開されています。 Text *textproto.Conn // contains filtered or unexported fields }
ClientはSMTPサーバーへのクライアント接続を表します。
func Dial ¶
Dialはaddrに指定されたポート付きのSMTPサーバーに接続された新しいClientを返します。 addrは"mail.example.com:smtp"のような形式である必要があります。
func (*Client) Auth ¶
Authは提供された認証メカニズムを使用してクライアントを認証します。 認証に失敗した場合、接続は閉じられます。 この機能は、AUTH拡張機能をサポートしているサーバーのみが広告しています。
func (*Client) Data ¶
func (c *Client) Data() (io.WriteCloser, error)
DataはサーバーにDATAコマンドを送信し、メールのヘッダーと本文を書き込むために使用できるライターを返します。呼び出し元は、cの他のメソッドを呼び出す前にライターを閉じる必要があります。Dataの呼び出しは、一つ以上のRcptの呼び出しに先行する必要があります。
func (*Client) Extension ¶
Extensionはサーバーが対応している拡張機能かどうかを報告します。 拡張機能名は大文字小文字を区別しません。もし拡張機能が対応されている場合、 Extensionは拡張機能に対してサーバーが指定する任意のパラメータを含む文字列も返します。
func (*Client) Hello ¶ added in v1.1.0
Helloメソッドは、指定されたホスト名としてサーバーにHELOまたはEHLOを送信します。 クライアントが使用するホスト名を制御する必要がある場合にのみ、このメソッドを呼び出す必要があります。 それ以外の場合は、クライアントは自動的に「localhost」として自己紹介します。 Helloメソッドを呼び出す場合は、他のメソッドのいずれかを呼び出す前に呼び出す必要があります。
func (*Client) Mail ¶
Mailは提供されたメールアドレスを使用してサーバーにMAILコマンドを発行します。 サーバーが8BITMIME拡張をサポートしている場合、MailはBODY=8BITMIMEパラメータを追加します。 サーバーがSMTPUTF8拡張をサポートしている場合、MailはSMTPUTF8パラメータを追加します。 これにより、メールのトランザクションが開始され、その後に1つ以上のRcpt呼び出しが続きます。
func (*Client) Rcpt ¶
Rcptは提供されたメールアドレスを使用してサーバーにRCPTコマンドを発行します。 Rcptの呼び出しは、Mailの呼び出しの前に行われなければならず、Dataの呼び出しまたは別のRcptの呼び出しの後に続く場合があります。
func (*Client) StartTLS ¶
StartTLSはSTARTTLSコマンドを送信し、以降のすべての通信を暗号化します。 この機能をサポートするのは、STARTTLS拡張機能を広告するサーバーのみです。
func (*Client) TLSConnectionState ¶ added in v1.5.0
func (c *Client) TLSConnectionState() (state tls.ConnectionState, ok bool)
TLSConnectionState はクライアントのTLS接続状態を返します。 StartTLS が成功しなかった場合、返り値はゼロ値になります。
type ServerInfo ¶
ServerInfoはSMTPサーバーの情報を記録します。