-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
52 lines (44 loc) · 1018 Bytes
/
mail.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
package mail
import (
"errors"
"fmt"
"net/smtp"
"strings"
)
// Mail 邮件配置
type Mail struct {
Nickname string `json:"nickname"`
User string `json:"user"`
Password string `json:"password"`
Host string `json:"host"`
Port string `json:"port"`
}
// Auth 身份认证
func (m *Mail) Auth() smtp.Auth {
return smtp.PlainAuth("", m.User, m.Password, m.Host)
}
func (m *Mail) checkParam() error {
if m.Host == "" || m.Nickname == "" || m.Password == "" || m.Port == "" || m.User == "" {
return errors.New("请先配置邮箱")
}
return nil
}
// SendMail 发送邮件
func (m *Mail) SendMail(subject string, to []string, body string) (err error) {
err = m.checkParam()
if err != nil {
return
}
template := `To:%s
From:%s<%s>
Subject:%s
Content-Type: text/html; charset=UTF-8
<html>
<body>
%s
<body>
</html>
`
msg := []byte(fmt.Sprintf(template, strings.Join(to, ","), m.Nickname, m.User, subject, body))
return smtp.SendMail(m.Host+":"+m.Port, m.Auth(), m.User, to, msg)
}