smtp

package
v1.21.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 4 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SendMail

func SendMail(addr string, a Auth, from string, to []string, msg []byte) error

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

func CRAMMD5Auth(username, secret string) Auth

CRAMMD5AuthはRFC 2195で定義されたCRAM-MD5認証メカニズムを実装するAuthを返します。 返されたAuthは、ユーザー名と秘密情報を使用して、チャレンジ-レスポンスメカニズムを使ってサーバーに認証します。

func PlainAuth

func PlainAuth(identity, username, password, host string) Auth

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

func Dial(addr string) (*Client, error)

Dialはaddrに指定されたポート付きのSMTPサーバーに接続された新しいClientを返します。 addrは"mail.example.com:smtp"のような形式である必要があります。

func NewClient

func NewClient(conn net.Conn, host string) (*Client, error)

NewClient は既存の接続とホストを使用して新しいクライアントを返します。認証時に使用するサーバー名です。

func (*Client) Auth

func (c *Client) Auth(a Auth) error

Authは提供された認証メカニズムを使用してクライアントを認証します。 認証に失敗した場合、接続は閉じられます。 この機能は、AUTH拡張機能をサポートしているサーバーのみが広告しています。

func (*Client) Close added in v1.2.0

func (c *Client) Close() error

Closeは接続をクローズします。

func (*Client) Data

func (c *Client) Data() (io.WriteCloser, error)

DataはサーバーにDATAコマンドを送信し、メールのヘッダーと本文を書き込むために使用できるライターを返します。呼び出し元は、cの他のメソッドを呼び出す前にライターを閉じる必要があります。Dataの呼び出しは、一つ以上のRcptの呼び出しに先行する必要があります。

func (*Client) Extension

func (c *Client) Extension(ext string) (bool, string)

Extensionはサーバーが対応している拡張機能かどうかを報告します。 拡張機能名は大文字小文字を区別しません。もし拡張機能が対応されている場合、 Extensionは拡張機能に対してサーバーが指定する任意のパラメータを含む文字列も返します。

func (*Client) Hello added in v1.1.0

func (c *Client) Hello(localName string) error

Helloメソッドは、指定されたホスト名としてサーバーにHELOまたはEHLOを送信します。 クライアントが使用するホスト名を制御する必要がある場合にのみ、このメソッドを呼び出す必要があります。 それ以外の場合は、クライアントは自動的に「localhost」として自己紹介します。 Helloメソッドを呼び出す場合は、他のメソッドのいずれかを呼び出す前に呼び出す必要があります。

func (*Client) Mail

func (c *Client) Mail(from string) error

Mailは提供されたメールアドレスを使用してサーバーにMAILコマンドを発行します。 サーバーが8BITMIME拡張をサポートしている場合、MailはBODY=8BITMIMEパラメータを追加します。 サーバーがSMTPUTF8拡張をサポートしている場合、MailはSMTPUTF8パラメータを追加します。 これにより、メールのトランザクションが開始され、その後に1つ以上のRcpt呼び出しが続きます。

func (*Client) Noop added in v1.10.0

func (c *Client) Noop() error

NoopはサーバーにNOOPコマンドを送信します。これによってサーバーとの接続が正常であることを確認します。

func (*Client) Quit

func (c *Client) Quit() error

QuitはQUITコマンドを送信し、サーバーへの接続を閉じます。

func (*Client) Rcpt

func (c *Client) Rcpt(to string) error

Rcptは提供されたメールアドレスを使用してサーバーにRCPTコマンドを発行します。 Rcptの呼び出しは、Mailの呼び出しの前に行われなければならず、Dataの呼び出しまたは別のRcptの呼び出しの後に続く場合があります。

func (*Client) Reset

func (c *Client) Reset() error

Resetは、現在のメールトランザクションを中止し、サーバーにRSETコマンドを送信します。

func (*Client) StartTLS

func (c *Client) StartTLS(config *tls.Config) error

StartTLSはSTARTTLSコマンドを送信し、以降のすべての通信を暗号化します。 この機能をサポートするのは、STARTTLS拡張機能を広告するサーバーのみです。

func (*Client) TLSConnectionState added in v1.5.0

func (c *Client) TLSConnectionState() (state tls.ConnectionState, ok bool)

TLSConnectionState はクライアントのTLS接続状態を返します。 StartTLS が成功しなかった場合、返り値はゼロ値になります。

func (*Client) Verify

func (c *Client) Verify(addr string) error

Verifyはサーバー上でメールアドレスの妥当性をチェックします。 Verifyがnilを返す場合、アドレスは有効です。非nilの返り値は 必ずしも無効なアドレスを示すわけではありません。セキュリティ上の理由から、 多くのサーバーはアドレスの検証を行わない場合があります。

type ServerInfo

type ServerInfo struct {
	Name string
	TLS  bool
	Auth []string
}

ServerInfoはSMTPサーバーの情報を記録します。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL