forked from domodwyer/mailyak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mime.go
163 lines (127 loc) · 3.62 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
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
package mailyak
import (
"bytes"
"crypto/rand"
"fmt"
"io"
"mime/multipart"
"mime/quotedprintable"
"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)
}
// randomBoundary returns a random hexadecimal string used for separating MIME
// parts.
//
// The returned string must be sufficiently random to prevent malicious users
// from performing content injection attacks.
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 using mb and ab as MIME
// boundaries, and returns the generated MIME data as a buffer.
func (m *MailYak) buildMimeWithBoundaries(mb, ab string) (*bytes.Buffer, error) {
var buf bytes.Buffer
if err := m.writeHeaders(&buf); err != nil {
return nil, err
}
// Start our multipart/mixed part
mixed := multipart.NewWriter(&buf)
if err := mixed.SetBoundary(mb); err != nil {
return nil, err
}
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, Date, Reply-To, From, To and Subject headers,
// plus any custom headers set via AddHeader().
func (m *MailYak) writeHeaders(buf io.Writer) error {
if _, err := buf.Write([]byte(m.fromHeader())); err != nil {
return err
}
if _, err := buf.Write([]byte("Mime-Version: 1.0\r\n")); err != nil {
return err
}
fmt.Fprintf(buf, "Date: %s\r\n", m.date)
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)
}
for _, cc := range m.ccAddrs {
fmt.Fprintf(buf, "CC: %s\r\n", cc)
}
if m.writeBccHeader {
for _, bcc := range m.bccAddrs {
fmt.Fprintf(buf, "BCC: %s\r\n", bcc)
}
}
for k, v := range m.headers {
fmt.Fprintf(buf, "%s: %s\r\n", k, v)
}
return nil
}
// 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()
if err := alt.SetBoundary(boundary); err != nil {
return err
}
var err error
writePart := func(ctype string, data []byte) {
if len(data) == 0 || err != nil {
return
}
c := fmt.Sprintf("%s; charset=UTF-8", ctype)
var part io.Writer
part, err = alt.CreatePart(textproto.MIMEHeader{"Content-Type": {c}, "Content-Transfer-Encoding": {"quoted-printable"}})
if err != nil {
return
}
var buf bytes.Buffer
qpw := quotedprintable.NewWriter(&buf)
_, err = qpw.Write(data)
qpw.Close()
_, err = part.Write(buf.Bytes())
}
writePart("text/plain", m.plain.Bytes())
writePart("text/html", m.html.Bytes())
return err
}