-
Notifications
You must be signed in to change notification settings - Fork 3
/
email.go
53 lines (38 loc) · 1.18 KB
/
email.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package email
import (
"crypto/tls"
"github.com/iimeta/iim-server/internal/config"
"gopkg.in/gomail.v2"
)
type Option struct {
To []string // 收件人
Subject string // 邮件主题
Body string // 邮件正文
}
type OptionFunc func(msg *gomail.Message)
func SendMail(email *Option, opt ...OptionFunc) error {
m := gomail.NewMessage()
// 这种方式可以添加别名, 即“XX官方”
m.SetHeader("From", m.FormatAddress(config.Cfg.Email.UserName, config.Cfg.Email.FromName))
if len(email.To) > 0 {
m.SetHeader("To", email.To...)
}
if len(email.Subject) > 0 {
m.SetHeader("Subject", email.Subject)
}
if len(email.Body) > 0 {
m.SetBody("text/html", email.Body)
}
// m.SetHeader("Cc", m.FormatAddress("xxxx@foxmail.com", "收件人")) //抄送
// m.SetHeader("Bcc", m.FormatAddress("xxxx@gmail.com", "收件人")) //暗送
for _, o := range opt {
o(m)
}
return do(m)
}
func do(msg *gomail.Message) error {
dialer := gomail.NewDialer(config.Cfg.Email.Host, config.Cfg.Email.Port, config.Cfg.Email.UserName, config.Cfg.Email.Password)
// 自动开启SSL
dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
return dialer.DialAndSend(msg)
}