Gomail是一个简单有效的电子邮件发送包。它经过了良好的测试和文档化。
Gomail只能使用SMTP服务器发送电子邮件。但是这个API是灵活的,而且它很容易实现使用本地后缀、API等发送电子邮件的其他方法。
安装
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 (守护进程)
一个后台程序用来监听 channel 和发送进来的消息
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 := <-ch:
if !ok {
return
}
if !open {
if s, err = d.Dial(); err != nil {
panic(err)
}
open = true
}
if err := gomail.Send(s, m); err != nil {
log.Print(err)
}
// Close the connection to the SMTP server if no email was sent in
// the last 30 seconds.
case <-time.After(30 * time.Second):
if open {
if err := s.Close(); err != nil {
panic(err)
}
open = false
}
}
}
}()
// Use the channel in your program to send emails.
// Close the channel to stop the mail daemon.
close(ch)
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服务器发送电子邮件
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发送电子邮件
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对象