-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
230 lines (212 loc) · 4.7 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//Package sendmail use net/smtp to send email
package sendmail
import (
"crypto/tls"
"encoding/base64"
"errors"
"mime"
"net"
"net/mail"
"net/smtp"
"strings"
)
var (
//ErrNoMailFrom address of from user is empty
ErrNoMailFrom = errors.New("from field is empty")
//ErrNoMailTo address of to is empty
ErrNoMailTo = errors.New("to field is empty")
)
func parseAddress(a []string) ([]*mail.Address, error) {
addrs := make([]*mail.Address, len(a))
for i := 0; i < len(a); i++ {
addr, err := mail.ParseAddress(a[i])
if err != nil {
return nil, err
}
addrs[i] = addr
}
return addrs, nil
}
func newMessage(from *mail.Address, to, cc, bcc []*mail.Address, sub string, msg []byte) (string, error) {
var buf strings.Builder
// write subject
buf.WriteString("Subject: ")
buf.WriteString(mime.BEncoding.Encode("utf-8", sub))
buf.WriteString("\r\n")
//write mail from
buf.WriteString("From: ")
buf.WriteString(from.String())
buf.WriteString("\r\n")
// write rcpt to
buf.WriteString("To: ")
for i := 0; i < len(to); i++ {
buf.WriteString(to[i].String())
if i != len(to)-1 {
buf.WriteByte(',')
}
}
buf.WriteString("\r\n")
// write cc
buf.WriteString("Cc: ")
for i := 0; i < len(cc); i++ {
buf.WriteString(cc[i].String())
if i != len(to)-1 {
buf.WriteByte(',')
}
}
buf.WriteString("\r\n")
// write Bcc
buf.WriteString("Bcc: ")
for i := 0; i < len(bcc); i++ {
buf.WriteString(bcc[i].String())
if i != len(to)-1 {
buf.WriteByte(',')
}
}
buf.WriteString("\r\n")
// write content-type
buf.WriteString("Content-Type: text/html; charset=UTF-8\r\n")
buf.WriteString("Content-Transfer-Encoding: base64\r\n")
buf.WriteString("\r\n")
buf.WriteString(base64.StdEncoding.EncodeToString(msg))
buf.WriteString("\r\n")
return buf.String(), nil
}
//Sendmail use smtp.SendMail
func Sendmail(addr, from, password string, to, cc, bcc []string, sub string, msg []byte) error {
//check addr with net.SplitHostPort
host, _, err := net.SplitHostPort(addr)
if err != nil {
return err
}
if from == "" {
return ErrNoMailFrom
}
if len(to) < 1 {
return ErrNoMailTo
}
mailfrom, err := mail.ParseAddress(from)
if err != nil {
return err
}
rcptto, err := parseAddress(to)
if err != nil {
return err
}
ccto, err := parseAddress(cc)
if err != nil {
return err
}
bccto, err := parseAddress(bcc)
if err != nil {
return err
}
content, err := newMessage(mailfrom, rcptto, ccto, bccto, sub, msg)
if err != nil {
return err
}
rcptto = append(rcptto, ccto...)
rcptto = append(rcptto, bccto...)
mailto := make([]string, len(rcptto))
for i := 0; i < len(rcptto); i++ {
mailto[i] = rcptto[i].Address
}
// fmt.Println(mailfrom.Address)
// fmt.Println(password)
if password == "" {
if err := smtp.SendMail(addr, nil, mailfrom.Address, mailto, []byte(content)); err != nil {
return err
}
} else {
//PlainAuth
a := smtp.PlainAuth("", mailfrom.Address, password, host)
if err := smtp.SendMail(addr, a, mailfrom.Address, mailto, []byte(content)); err != nil {
return err
}
}
return nil
}
//SkipVerifyTLS sendmail skip TLS verify, it only should be used on localhost
func SkipVerifyTLS(addr, from, password string, to, cc, bcc []string, sub string, msg []byte) error {
//check addr with net.SplitHostPort
host, _, err := net.SplitHostPort(addr)
if err != nil {
return err
}
if from == "" {
return ErrNoMailFrom
}
if len(to) < 1 {
return ErrNoMailTo
}
c, err := smtp.Dial(addr)
if err != nil {
return err
}
defer c.Close()
//skip tls verify
config := &tls.Config{
InsecureSkipVerify: true,
}
if err = c.StartTLS(config); err != nil {
return err
}
mailfrom, err := mail.ParseAddress(from)
if err != nil {
return err
}
//auth password
if password != "" {
a := smtp.PlainAuth("", mailfrom.Address, password, host)
if err := c.Auth(a); err != nil {
return err
}
}
if err = c.Mail(mailfrom.Address); err != nil {
return err
}
rcptto, err := parseAddress(to)
if err != nil {
return err
}
ccto, err := parseAddress(cc)
if err != nil {
return err
}
bccto, err := parseAddress(bcc)
if err != nil {
return err
}
for _, rcpt := range rcptto {
if err := c.Rcpt(rcpt.Address); err != nil {
return err
}
}
for _, rcpt := range ccto {
if err := c.Rcpt(rcpt.Address); err != nil {
return err
}
}
for _, rcpt := range bccto {
if err := c.Rcpt(rcpt.Address); err != nil {
return err
}
}
content, err := newMessage(mailfrom, rcptto, ccto, bccto, sub, msg)
if err != nil {
return err
}
//wc is io.WriteCloser
wc, err := c.Data()
if err != nil {
return err
}
// write email content to wc
if _, err := wc.Write([]byte(content)); err != nil {
return err
}
if err = wc.Close(); err != nil {
return err
}
return c.Quit()
}