-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
322 lines (275 loc) · 7.53 KB
/
email.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
package email
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"mime/quotedprintable"
"net/http"
"strings"
"time"
)
const DefaultBoundary = "110000000000863a1705ddeb4f86"
func NewEmailBuilder() *EmailBuilder {
return &EmailBuilder{
Headers: make(http.Header),
PlainHeaders: make(http.Header),
HTMLHeaders: make(http.Header),
}
}
// EmailBuilder helps build a multipart email message
// in MIME (Multipurpose Internet Mail Extensions) encoding.
type EmailBuilder struct {
// Headers stores the custom key-value pairs of a MIME message.
Headers http.Header
// Boundary is the custom boundary.
// Used only if the Headers does not contain a Content-Type header.
// If the Headers contains a Content-Type header, the boundary
// will be parsed from the header value.
Boundary string
// PlainHeaders stores the custom key-value pairs for the plain body part.
PlainHeaders http.Header
// Plain is the encoded plain text body part in wire format without the trailing \r\n.
Plain bytes.Buffer
// HTMLHeaders stores the custom key-value pairs for the HTML body part.
HTMLHeaders http.Header
// HTML is the encoded HTML text body part in wire format without the trailing \r\n.
HTML bytes.Buffer
}
// SetFrom creates the From header.
func (b *EmailBuilder) SetFrom(from string) {
b.Headers.Set("From", from)
}
// SetTo creates the To header.
func (b *EmailBuilder) SetTo(to []string) {
buf := &bytes.Buffer{}
for i, s := range to {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(s)
}
b.Headers.Set("To", buf.String())
}
// SetSubject creates the Subject header with the specified s value.
func (b *EmailBuilder) SetSubject(s string) {
b.Headers.Set("Subject", s)
}
// SetPlainCharset creates the plain text Content-Type header
// with the specified s charset.
func (b *EmailBuilder) SetPlainCharset(s string) {
b.PlainHeaders.Set("Content-Type", "text/plain; charset="+s)
}
// SetHTMLCharset creates the HTML text Content-Type header
// with the specified s charset.
func (b *EmailBuilder) SetHTMLCharset(s string) {
b.HTMLHeaders.Set("Content-Type", "text/html; charset="+s)
}
// EncodeBase64Plain encodes s using base64 encoding
// and writes it to Plain buffer.
// Plain buffer will be reset to be empty before encoding,
// but the underlying storage will be retained.
func (b *EmailBuilder) EncodeBase64Plain(s []byte) {
b.Plain.Reset()
b.PlainHeaders.Set("Content-Transfer-Encoding", "base64")
encoder := base64.NewEncoder(base64.StdEncoding, &b.Plain)
encoder.Write(s)
encoder.Close()
}
// EncodeBase64HTML encodes s using base64 encoding
// and writes it to HTML buffer.
// HTML buffer will be reset to be empty before encoding,
// but the underlying storage will be retained.
func (b *EmailBuilder) EncodeBase64HTML(s []byte) {
b.HTML.Reset()
b.HTMLHeaders.Set("Content-Transfer-Encoding", "base64")
encoder := base64.NewEncoder(base64.StdEncoding, &b.HTML)
encoder.Write(s)
encoder.Close()
}
// EncodeQuotedPlain encodes s using quoted-printable encoding
// and writes it to Plain buffer.
// It limits line length to 76 characters.
// Plain buffer will be reset to be empty before encoding,
// but the underlying storage will be retained.
func (b *EmailBuilder) EncodeQuotedPlain(s []byte) error {
b.Plain.Reset()
b.PlainHeaders.Set("Content-Transfer-Encoding", "quoted-printable")
w := quotedprintable.NewWriter(&b.Plain)
_, err := w.Write(s)
if err != nil {
return err
}
return w.Close()
}
// EncodeQuotedHTML encodes s using quoted-printable encoding
// and writes it to HTML buffer.
// It limits line length to 76 characters.
// HTML buffer will be reset to be empty before encoding,
// but the underlying storage will be retained.
func (b *EmailBuilder) EncodeQuotedHTML(s []byte) error {
b.HTML.Reset()
b.HTMLHeaders.Set("Content-Transfer-Encoding", "quoted-printable")
w := quotedprintable.NewWriter(&b.HTML)
_, err := w.Write(s)
if err != nil {
return err
}
return w.Close()
}
// Write writes a MIME email in wire format.
func (b *EmailBuilder) Write(w io.Writer) error {
text := b.Plain.Len() > 0
html := b.HTML.Len() > 0
multipart := text && html
contentType := b.Headers.Get("Content-Type")
extraHeaders := make(http.Header)
if b.Headers.Get("MIME-Version") == "" {
extraHeaders.Set("MIME-Version", "1.0")
}
if b.Headers.Get("Date") == "" {
extraHeaders.Set("Date", time.Now().Format(time.RFC1123Z))
}
var boundary string
if multipart {
bo, err := b.BoundaryString()
if err != nil {
return err
}
boundary = bo
if contentType == "" {
extraHeaders.Set(
"Content-Type",
fmt.Sprintf(`multipart/alternative; boundary="%s"`, boundary),
)
}
}
err := b.Headers.Write(w)
if err != nil {
return err
}
err = extraHeaders.Write(w)
if err != nil {
return err
}
if multipart {
err = b.writeln(w)
if err != nil {
return err
}
err = b.writePartPlain(w, boundary, b.PlainHeaders)
if err != nil {
return err
}
err = b.writePartHTML(w, boundary, b.HTMLHeaders)
if err != nil {
return err
}
_, err = w.Write([]byte("--" + boundary + "--"))
return err
}
if html {
return b.writePartHTML(w, "", b.HTMLHeaders)
}
return b.writePartPlain(w, "", b.PlainHeaders)
}
func (b *EmailBuilder) writePartPlain(w io.Writer, boundary string, headers http.Header) error {
extraHeaders := make(http.Header)
if headers.Get("Content-Type") == "" {
extraHeaders.Set("Content-Type", "text/plain; charset=utf-8")
}
if boundary != "" {
_, err := w.Write([]byte("--" + boundary))
if err != nil {
return err
}
err = b.writeln(w)
if err != nil {
return err
}
}
err := headers.Write(w)
if err != nil {
return err
}
err = extraHeaders.Write(w)
if err != nil {
return err
}
err = b.writeln(w)
if err != nil {
return err
}
_, err = w.Write(b.Plain.Bytes())
if err != nil {
return err
}
return b.writeln(w)
}
func (b *EmailBuilder) writePartHTML(w io.Writer, boundary string, headers http.Header) error {
extraHeaders := make(http.Header)
if headers.Get("Content-Type") == "" {
extraHeaders.Set("Content-Type", "text/html; charset=utf-8")
}
if boundary != "" {
_, err := w.Write([]byte("--" + boundary))
if err != nil {
return err
}
err = b.writeln(w)
if err != nil {
return err
}
}
err := headers.Write(w)
if err != nil {
return err
}
err = extraHeaders.Write(w)
if err != nil {
return err
}
err = b.writeln(w)
if err != nil {
return err
}
_, err = w.Write(b.HTML.Bytes())
if err != nil {
return err
}
return b.writeln(w)
}
func (b *EmailBuilder) writeln(w io.Writer) error {
_, err := w.Write([]byte("\r\n"))
return err
}
// BoundaryString returns the boundary string.
// If a custom Content-Type header is specified in the Headers
// the boundary will be parsed from that header value.
// If a custom Boundary field is specified it will return that value.
// Otherwise it will return the DefaultBoundary.
func (b *EmailBuilder) BoundaryString() (string, error) {
contentType := b.Headers.Get("Content-Type")
if contentType != "" {
pattern := `boundary="`
i := strings.Index(contentType, pattern)
if i == -1 {
return "", fmt.Errorf("beginning of the boundary not found in Content-Type header")
}
i = i + len(pattern)
j := strings.Index(contentType[i:], `"`)
if j == -1 {
return "", fmt.Errorf("end of the boundary not found in Content-Type header")
}
boundary := contentType[i : i+j]
if boundary == "" {
return "", fmt.Errorf("empty boundary in Content-Type header")
}
return boundary, nil
}
boundary := b.Boundary
if boundary == "" {
boundary = DefaultBoundary
}
return boundary, nil
}