goメール
5029 ワード
Gomailは簡単で効率的な電子メール送信パッケージです.良好なテストとドキュメント化を経ています.GomailはSMTPサーバのみで電子メールを送信できます.しかし,このAPIは柔軟であり,ローカル接尾辞,APIなどを用いて電子メールを送信する他の方法を容易に実現できる.
インストール
Example ()
Example(デーモン)
チャネルと送信されたメッセージを傍受するためのバックグラウンド・プログラム
Example(リアルタイム通信)
受信者リストにカスタマイズされた時事通信を効率的に送信
Example (NoAuth)
ローカルSMTPサーバを使用したEメール送信
Example (NoSMTP)
APIまたはpostfixを使用してEメールを送信
result
いくつかの方法 がデフォルトで使用されます. である
インストール
go get gopkg.in/gomail.v2
Example ()
m := gomail.NewMessage()
m.SetHeader("From", "[email protected]")
m.SetHeader("To", "[email protected]", "[email protected]")
m.SetAddressHeader("Cc", "[email protected]", "Dan")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/html", "Hello Bob and Cora!")
m.Attach("/home/Alex/lolcat.jpg")
d := gomail.NewDialer("smtp.example.com", 587, "user", "123456")
// Send the email to Bob, Cora and Dan.
if err := d.DialAndSend(m); err != nil {
panic(err)
}
Example(デーモン)
チャネルと送信されたメッセージを傍受するためのバックグラウンド・プログラム
ch := make(chan *gomail.Message)
go func() {
d := gomail.NewDialer("smtp.example.com", 587, "user", "123456")
var s gomail.SendCloser
var err error
open := false
for {
select {
case m, ok :=
Example(リアルタイム通信)
受信者リストにカスタマイズされた時事通信を効率的に送信
// The list of recipients.
var list []struct {
Name string
Address string
}
d := gomail.NewDialer("smtp.example.com", 587, "user", "123456")
s, err := d.Dial()
if err != nil {
panic(err)
}
m := gomail.NewMessage()
for _, r := range list {
m.SetHeader("From", "[email protected]")
m.SetAddressHeader("To", r.Address, r.Name)
m.SetHeader("Subject", "Newsletter #1")
m.SetBody("text/html", fmt.Sprintf("Hello %s!", r.Name))
if err := gomail.Send(s, m); err != nil {
log.Printf("Could not send email to %q: %v", r.Address, err)
}
m.Reset()
}
Example (NoAuth)
ローカルSMTPサーバを使用したEメール送信
m := gomail.NewMessage()
m.SetHeader("From", "[email protected]")
m.SetHeader("To", "[email protected]")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/plain", "Hello!")
d := gomail.Dialer{Host: "localhost", Port: 587}
if err := d.DialAndSend(m); err != nil {
panic(err)
}
Example (NoSMTP)
APIまたはpostfixを使用してEメールを送信
m := gomail.NewMessage()
m.SetHeader("From", "[email protected]")
m.SetHeader("To", "[email protected]")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/plain", "Hello!")
s := gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error {
// Implements you email-sending function, for example by calling
// an API, or running postfix, etc.
fmt.Println("From:", from)
fmt.Println("To:", to)
return nil
})
if err := gomail.Send(s, m); err != nil {
panic(err)
}
result
From: [email protected]
To: [[email protected]]
いくつかの方法
func NewMessage(settings ...MessageSetting) *Message
NewMessageは、新しいメッセージを作成します.UTF-8およびquoted-printable符号化func (m *Message) SetHeader(field string, value ...string)
SetHeader sets a value to the given header field. m.SetHeader("Subject", "Hello!")
func (m *Message) SetHeaders(h map[string][]string)
SetHeaders sets the message headers. m.SetHeaders(map[string][]string{
"From": {m.FormatAddress("[email protected]", "Alex")},
"To": {"[email protected]", "[email protected]"},
"Subject": {"Hello"},
})
func (m *Message) SetAddressHeader(field, address, name string)
SetAddressHeader sets an address to the given header field. m.SetAddressHeader("To", "[email protected]", "Bob")
func (m *Message) SetBody(contentType, body string, settings ...PartSetting)
SetBodyメッセージのマスターを設定します.以前にSetBody、AddAlternative、またはAddAlternative writerによって設定されていたものを置き換えます.m.SetBody("text/plain", "Hello!")
func (m *Message) Reset()
メッセージを再利用できるようにリセットします.このメッセージは、以前の設定を保持するため、NewMessageが呼び出された後も同じ状態func NewDialer(host string, port int, username, password string) *Dialer
は、新しいSMTPダイヤルを返します.指定されたパラメータは、SMTPサーバに接続するために使用されるfunc (d *Dialer) DialAndSend(m ...*Message) error
SMTPサーバへの接続を開き、所定の電子メールを送信し、接続を閉じるfunc Send(s Sender, msg ...*Message) error
指定された送信者を使用して電子メールを送信type SendFunc func(from string, to []string, msg io.WriterTo) error
SendFuncは、指定されたアドレスに電子メールを送信する関数SendFuncタイプのアダプタであり、通常の機能を電子メール送信機として使用することができる.fが適切な署名を有する関数である場合、SendFunc(f)は、fを呼び出すSenderオブジェクト