forked from domodwyer/mailyak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mime.go
120 lines (91 loc) · 2.54 KB
/
mime.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
package mailyak
import (
"bytes"
"crypto/rand"
"fmt"
"io"
"mime/multipart"
"net/textproto"
)
func (m *MailYak) buildMime() (*bytes.Buffer, error) {
mb, err := randomBoundary()
if err != nil {
return nil, err
}
ab, err := randomBoundary()
if err != nil {
return nil, err
}
return m.buildMimeWithBoundaries(mb, ab)
}
func randomBoundary() (string, error) {
buf := make([]byte, 30)
_, err := io.ReadFull(rand.Reader, buf)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", buf), nil
}
// buildMimeWithBoundaries creates the MIME message and returns it as a buffer
func (m *MailYak) buildMimeWithBoundaries(mb, ab string) (*bytes.Buffer, error) {
var buf bytes.Buffer
m.writeHeaders(&buf)
// Start our multipart/mixed part
mixed := multipart.NewWriter(&buf)
mixed.SetBoundary(mb)
defer mixed.Close()
fmt.Fprintf(&buf, "Content-Type: multipart/mixed;\r\n\tboundary=\"%s\"; charset=UTF-8\r\n\r\n", mixed.Boundary())
ctype := fmt.Sprintf("multipart/alternative;\r\n\tboundary=\"%s\"", ab)
altPart, err := mixed.CreatePart(textproto.MIMEHeader{"Content-Type": {ctype}})
if err != nil {
return nil, err
}
if err := m.writeBody(altPart, ab); err != nil {
return nil, err
}
if err := m.writeAttachments(mixed, lineSplitterBuilder{}); err != nil {
return nil, err
}
return &buf, nil
}
// writeHeaders writes the Mime-Version, Reply-To, From, To and Subject headers
func (m MailYak) writeHeaders(buf io.Writer) {
buf.Write([]byte(m.fromHeader()))
buf.Write([]byte("Mime-Version: 1.0\r\n"))
if m.replyTo != "" {
fmt.Fprintf(buf, "Reply-To: %s\r\n", m.replyTo)
}
fmt.Fprintf(buf, "Subject: %s\r\n", m.subject)
for _, to := range m.toAddrs {
fmt.Fprintf(buf, "To: %s\r\n", to)
}
}
// fromHeader returns a correctly formatted From header, optionally with a name
// component
func (m MailYak) fromHeader() string {
if m.fromName == "" {
return fmt.Sprintf("From: %s\r\n", m.fromAddr)
}
return fmt.Sprintf("From: %s <%s>\r\n", m.fromName, m.fromAddr)
}
// writeBody writes the text/plain and text/html mime parts
func (m MailYak) writeBody(w io.Writer, boundary string) error {
alt := multipart.NewWriter(w)
defer alt.Close()
alt.SetBoundary(boundary)
var err error
writePart := func(ctype string, data []byte) {
if len(data) == 0 || err != nil {
return
}
c := fmt.Sprintf("%s; charset=UTF-8", ctype)
part, err := alt.CreatePart(textproto.MIMEHeader{"Content-Type": {c}})
if err != nil {
return
}
part.Write(data)
}
writePart("text/plain", m.plain)
writePart("text/html", m.html)
return err
}