简介
Gomail是一个简单而高效的发送电子邮件的软件包。它经过了良好的测试和记录。
Gomail只能通过SMTP服务器发送邮件。但是API是灵活的,它很容易实现使用本地Postfix、API等发送电子邮件的其他方法。
它是使用gopkg版本。因此,我保证在每个版本中永远不会有向后不兼容的更改。
它需要Go 1.2或更新版本。在Go 1.5中,没有使用外部依赖项。
特性
Gomail 支持:
- Attachments(附件)
 
- Embedded images(嵌入式图片)
 
- HTML and text templates(HTML和Text模板)
 
- Automatic encoding of special characters(特殊字符自动编码)
 
- SSL and TLS
 
- Sending multiple emails with the same SMTP connection(使用同一个SMTP连接发送多个邮件)
 
下载
1
  | 
go get gopkg.in/gomail.v2
  | 
 
示例
示例1
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  | 
# 创建邮件消息体m
m := gomail.NewMessage() 
#设置发送人
m.SetHeader("From", "alex@example.com")
#设置接收人
m.SetHeader("To", "bob@example.com", "cora@example.com")
#设置后面的地址给 给定报文头Cc
m.SetAddressHeader("Cc", "dan@example.com", "Dan")
#设置邮件主题
m.SetHeader("Subject", "Hello!")
#设置邮件内容,格式为HTML
m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
#添加附件
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)
}
  | 
 
示例 Daemon
监听通道并发送所有传入消息的守护进程。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  | 
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)
			}
		// 如果没有发送电子邮件,请30秒后关闭与SMTP服务器的连接。
		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)
  | 
 
示例3
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  | 
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", "no-reply@example.com")
	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()
}
  | 
 
使用本地smtp服务器发送邮件
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  | 
m := gomail.NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")
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)
}
  | 
 
示例 NoSMTP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
  | 
使用API或后缀发送电子邮件。
m := gomail.NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/plain", "Hello!")
s := gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error {
	// 实现电子邮件发送功能,例如通过调用一个API,或运行后缀等。
	fmt.Println("From:", from)
	fmt.Println("To:", to)
	return nil
})
if err := gomail.Send(s, m); err != nil {
	panic(err)
}
  | 
 
案例
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  | 
package main
import (
	"crypto/tls"
	"fmt"
	"gopkg.in/gomail.v2"
)
func main()  {
	var sendTo = []string{
		"xys_1993l@163.com",
	}
	sendTo = append(sendTo,"xieys@lingcb.com")
	m := gomail.NewMessage()
	m.SetHeader("From", "service@lingcb.com")
	m.SetHeader("To", sendTo...)
	m.SetHeader("Subject", "RDS实例 : 测试 慢查询统计")
	m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
	d := gomail.NewDialer("smtp.exmail.qq.com",465,"service@lingcb.com","xxxxxxx")
	d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
	s,err := d.Dial()
	if err != nil{
		panic(err)
	}
	fmt.Println(gomail.Send(s,m))
	//d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
	//fmt.Println(d.DialAndSend(m))
}
  | 
 
常见问题
x509 : 由未知权威机构签署的证书
如果出现此错误,则表示运行Gomail的客户端认为SMTP服务器使用的证书无效。作为一个快速的解决方案,你可以使用SetTLSConfig绕过对服务器证书链和主机名的验证:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
  | 
package main
import (
	"crypto/tls"
	"gopkg.in/gomail.v2"
)
func main() {
	d := gomail.NewDialer("smtp.example.com", 587, "user", "123456")
	d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
    // Send emails using d.
}
  | 
 
但是,请注意,这是不安全的,不应该在生产中使用。