/
mail_message.go
490 lines (424 loc) · 11.8 KB
/
mail_message.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package delayed_job
import (
"bufio"
"bytes"
"crypto/md5"
"encoding/base64"
"errors"
"fmt"
"io"
"math"
"mime"
"mime/multipart"
qp "mime/quotedprintable"
"net"
"net/mail"
"net/textproto"
"path/filepath"
"strings"
"sync/atomic"
"time"
"github.com/runner-mei/delayed_job/smtp"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
const maxLineLen = 76 // RFC 2045
const crlf = "\r\n"
type MailMessage struct {
From mail.Address // if From.Address is empty, Config.DefaultFrom will be used
To []*mail.Address
Cc []*mail.Address
Bcc []*mail.Address
Subject string
ContentText string
ContentHtml string
Attachments []Attachment
}
type Attachment struct {
Name string
Content io.Reader
}
func toMailString(addr *mail.Address) string {
if 0 == len(addr.Name) {
return addr.Address
}
return addr.String()
}
// http://tools.ietf.org/html/rfc822
// http://tools.ietf.org/html/rfc2821
func (self *MailMessage) Bytes() ([]byte, error) {
from := self.From.String()
buf := bytes.NewBuffer(make([]byte, 0, 10240))
write := func(what string, recipients []*mail.Address) {
if nil == recipients {
return
}
if 0 == len(recipients) {
return
}
for i := range recipients {
if 0 == i {
buf.WriteString(what)
} else {
buf.WriteString(", ")
}
buf.WriteString(toMailString(recipients[i]))
}
buf.WriteString(crlf)
}
fmt.Fprintf(buf, "From: %s%s", from, crlf)
write("To: ", self.To)
write("Cc: ", self.Cc)
write("Bcc: ", self.Bcc)
fmt.Fprintf(buf, "Date: %s%s", time.Now().UTC().Format(time.RFC1123Z), crlf)
fmt.Fprintf(buf, "Subject: %s%s", encodeSubject(self.Subject), crlf)
var parts *Multipart
var alternative *Multipart
if len(self.Attachments) > 0 {
parts = NewMultipart(buf, "multipart/mixed")
var e error
alternative, e = parts.AddMultipart("multipart/alternative")
if nil != e {
return nil, e
}
} else {
alternative = NewMultipart(buf, "multipart/alternative")
parts = alternative
}
if "" != self.ContentHtml {
alternative.AddText("text/html; charset=\"utf-8\"", self.ContentHtml)
}
if "" != self.ContentText {
alternative.AddText("text/plain; charset=\"utf-8\"", self.ContentText)
}
alternative.Close()
if len(self.Attachments) > 0 {
for _, attachment := range self.Attachments {
e := parts.AddAttachment(AttachmentSytle,
attachment.Name, "", attachment.Content)
if nil != e {
return nil, e
}
}
parts.Close()
}
return buf.Bytes(), nil
}
func (self *MailMessage) Send(smtpServer string, auth smtp.Auth, useTLS smtp.TLSMethod, useFQDN bool) error {
if nil == self.To || 0 == len(self.To) {
return errors.New("'to_address' is missing")
}
if 0 == len(smtpServer) {
smtpServer = *defaultSmtpServer
if 0 == len(smtpServer) {
return errors.New("'smtp_server' is missing or default 'smtp_server' is not set")
}
}
if _, _, err := net.SplitHostPort(smtpServer); err != nil {
smtpServer = net.JoinHostPort(smtpServer, "25")
}
to := make([]string, 0, len(self.To))
for _, addr := range self.To {
if addr == nil {
continue
}
to = append(to, addr.Address)
}
from := self.From.Address
if 0 == len(from) {
return errors.New("'from_address' is missing or default 'from_address' is not set")
}
body, e := self.Bytes()
if nil != e {
return e
}
e = smtp.SendMail(smtpServer, auth, from, to, body, useTLS, useFQDN, nil)
if nil != e {
err := smtp.SendMail(smtpServer, nil, from, to, body, useTLS, useFQDN, nil)
if nil == err {
return nil
}
}
return e
}
func encodeSubject(txt string) string {
switch *default_mail_subject_encoding {
case "gb2312_base64":
return base64StringWithGB2312(txt)
case "gb2312_qp":
return qpStringWithGB2312(txt)
case "gb2312":
s, _, e := transform.String(simplifiedchinese.GB18030.NewEncoder(), txt)
if nil != e {
return qpString(txt)
}
return s
case "utf8_qp":
return qpString(txt)
default:
return qpString(txt)
}
}
func base64StringWithGB2312(txt string) string {
buf := bytes.NewBufferString("=?GB2312?B?")
bs, _, e := transform.Bytes(simplifiedchinese.GB18030.NewEncoder(), []byte(txt))
if nil != e {
return qpString(txt)
}
buf.WriteString(base64.StdEncoding.EncodeToString(bs))
buf.WriteString("?=")
return buf.String()
}
func qpStringWithGB2312(txt string) string {
buf := bytes.NewBufferString("=?GB2312?Q?")
bs, _, e := transform.Bytes(simplifiedchinese.GB18030.NewEncoder(), []byte(txt))
if nil != e {
return qpString(txt)
}
w := qp.NewWriter(buf)
w.Write(bs)
w.Close()
buf.WriteString("?=")
return buf.String()
}
func qpString(txt string) string {
buf := bytes.NewBufferString("=?utf-8?q?")
w := qp.NewWriter(buf)
io.WriteString(w, txt)
w.Close()
buf.WriteString("?=")
return buf.String()
}
var random_id_gen int32 = 0
func randomString() string {
h := md5.New()
io.WriteString(h, fmt.Sprintf("%d%d", time.Now().Nanosecond(), atomic.AddInt32(&random_id_gen, 1)))
return fmt.Sprintf("%x", h.Sum(nil))
}
// Generate aun unique boundary value
func (self *MailMessage) GetBoundary() string {
return randomString()
}
// AttachmentType indicates to the mail user agent how an attachment should be
// treated.
type AttachmentType string
const (
// Attachment indicates that the attachment should be offered as an optional
// download.
AttachmentSytle AttachmentType = "attachment"
// Inline indicates that the attachment should be rendered in the body of
// the message.
InlineSytle AttachmentType = "inline"
)
// Multipart repreents a multipart message body. It can other nest multiparts,
// texts, and attachments.
type Multipart struct {
writer *multipart.Writer
mediaType string
isClosed bool
header textproto.MIMEHeader
}
var ErrPartClosed = errors.New("mail: part has been closed")
// AddMultipart creates a nested part with mediaType and a randomly generated
// boundary. The returned nested part can then be used to add a text or
// an attachment.
//
// Example:
//
// alt, _ := part.AddMultipart("multipart/mixed")
// alt.AddText("text/plain", text)
// alt.AddAttachment("gopher.png", "", image)
// alt.Close()
func (p *Multipart) AddMultipart(mediaType string) (nested *Multipart, err error) {
if !strings.HasPrefix(mediaType, "multipart") {
return nil, errors.New("mail: mediaType must start with the word \"multipart\" as in multipart/mixed or multipart/alter")
}
if p.isClosed {
return nil, ErrPartClosed
}
boundary := randomString()
// Mutlipart management
var mimeType string
if strings.HasPrefix(mediaType, "multipart") {
mimeType = mime.FormatMediaType(
mediaType,
map[string]string{"boundary": boundary},
)
} else {
mimeType = mediaType
}
// Header
p.header = make(textproto.MIMEHeader)
p.header["Content-Type"] = []string{mimeType}
w, err := p.writer.CreatePart(p.header)
if err != nil {
return nil, err
}
nested = createPart(w, p.header, mediaType, boundary)
return nested, nil
}
// AddText applies quoted-printable encoding to the content of r before writing
// the encoded result in a new sub-part with media MIME type set to mediaType.
//
// Specifying the charset in the mediaType string is recommended
// ("plain/text; charset=utf-8").
func (p *Multipart) AddTextReader(mediaType string, r io.Reader) error {
if p.isClosed {
return ErrPartClosed
}
p.header = textproto.MIMEHeader(map[string][]string{
"Content-Type": {mediaType},
"Content-Transfer-Encoding": {"quoted-printable"},
})
w, err := p.writer.CreatePart(p.header)
if err != nil {
return err
}
reader := bufio.NewReader(r)
encoder := qp.NewWriter(w)
buffer := make([]byte, maxLineLen)
for {
read, err := reader.Read(buffer[:])
if err != nil {
if err != io.EOF {
return err
}
break
}
encoder.Write(buffer[:read])
}
encoder.Close()
fmt.Fprintf(w, crlf)
fmt.Fprintf(w, crlf)
return nil
}
func (p *Multipart) AddText(mediaType string, txt string) error {
if p.isClosed {
return ErrPartClosed
}
p.header = textproto.MIMEHeader(map[string][]string{
"Content-Type": {mediaType},
"Content-Transfer-Encoding": {"quoted-printable"},
})
w, err := p.writer.CreatePart(p.header)
if err != nil {
return err
}
encoder := qp.NewWriter(w)
_, err = io.WriteString(encoder, txt)
if err != nil {
return err
}
encoder.Close()
fmt.Fprintf(w, crlf)
fmt.Fprintf(w, crlf)
return nil
}
// AddAttachment encodes the content of r in base64 and writes it as an
// attachment of type attachType in this part.
//
// filename is the file name that will be suggested by the mail user agent to a
// user who would like to download the attachment. It's also the value to which
// the Content-ID header will be set. A name with an extension such as
// "report.docx" or "photo.jpg" is recommended. RFC 5987 is not supported, so
// the charset is restricted to ASCII characters.
//
// mediaType indicates the content type of the attachment. If an empty string is
// passed, mime.TypeByExtension will first be called to deduce a value from the
// extension of filemame before defaulting to "application/octet-stream".
//
// In the following example, the media MIME type will be set to "image/png"
// based on the ".png" extension of the filename "gopher.png":
//
// part.AddAttachment(Inline, "gopher.png", "", image)
func (p *Multipart) AddAttachment(attachType AttachmentType, filename, mediaType string, r io.Reader) (err error) {
if p.isClosed {
return ErrPartClosed
}
// Default Content-Type value
if mediaType == "" && filename != "" {
mediaType = mime.TypeByExtension(filepath.Ext(filename))
}
if mediaType == "" {
mediaType = "application/octet-stream"
}
header := textproto.MIMEHeader(map[string][]string{
"Content-Type": {mediaType},
"Content-ID": {"<" + filename + ">"},
"Content-Location": {filename},
"Content-Transfer-Encoding": {"base64"},
"Content-Disposition": {fmt.Sprintf("%s;\r\n\tfilename=%s;", attachType, qpString(filename))},
})
w, err := p.writer.CreatePart(header)
if err != nil {
return err
}
encoder := base64.NewEncoder(base64.StdEncoding, w)
data := bufio.NewReader(r)
buffer := make([]byte, int(math.Ceil(maxLineLen/4)*3))
for {
read, err := io.ReadAtLeast(data, buffer[:], len(buffer))
if err != nil {
if err == io.EOF {
break
} else if err != io.ErrUnexpectedEOF {
return err
}
}
if _, err := encoder.Write(buffer[:read]); err != nil {
return err
}
if read == len(buffer) {
fmt.Fprintf(w, crlf)
}
}
encoder.Close()
fmt.Fprintf(w, crlf)
return nil
}
// Header map of the part.
func (p *Multipart) Header() textproto.MIMEHeader {
return p.header
}
// Boundary separating the children of this part.
func (p *Multipart) Boundary() string {
return p.writer.Boundary()
}
// MediaType returns the media MIME type of this part.
func (p *Multipart) MediaType() string {
return p.mediaType
}
// Close adds a closing boundary to the part.
//
// Calling AddText, AddAttachment or AddMultipart on a closed part will return
// ErrPartClosed.
func (p *Multipart) Close() error {
if p.isClosed {
return ErrPartClosed
}
p.isClosed = true
return p.writer.Close()
}
func createPart(w io.Writer, header textproto.MIMEHeader, mediaType string, boundary string) *Multipart {
m := &Multipart{
writer: multipart.NewWriter(w),
header: header,
mediaType: mediaType,
}
m.writer.SetBoundary(boundary)
return m
}
// NewMultipart modifies msg to become a multipart message and returns the root
// part inside which other parts, texts and attachments can be nested.
//
// Example:
//
// multipart := NewMultipart("multipart/alternative", msg)
// multipart.AddPart("text/plain", text)
// multipart.AddPart("text/html", html)
// multipart.Close()
func NewMultipart(w io.Writer, mediaType string) (root *Multipart) {
boundary := randomString()
fmt.Fprintf(w, "Content-Type: %s; boundary=%s%s%s", mediaType, boundary, crlf, crlf)
return createPart(w, make(textproto.MIMEHeader), mediaType, boundary)
}