-
Notifications
You must be signed in to change notification settings - Fork 3
/
mime.go
269 lines (236 loc) · 6.58 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
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
package marcel
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"mime/multipart"
"mime/quotedprintable"
"net/textproto"
"time"
"unicode"
)
type Email struct {
To string
From string
ReplyTo string
ReturnPath string
Text string
HTML string
Subject string
Attachments []Attachment
}
type Attachment struct {
ContentType string
Data io.Reader
Filename string
}
// multipart/mixed
// |- multipart/related
// | |- multipart/alternative
// | | |- text/plain
// | | `- text/html
// | `- inlines..
// `- attachments..
func (email Email) ToMIME() ([]byte, error) {
dest := bytes.NewBuffer([]byte{})
if err := email.WriteMime(dest); err != nil {
return []byte{}, err
}
return io.ReadAll(dest)
}
func (email Email) WriteMime(dest io.Writer) error {
mixedContent := &bytes.Buffer{}
mixedWriter := multipart.NewWriter(mixedContent)
// related content, inside mixed
var relatedBoundary = "RELATED-" + mixedWriter.Boundary()
mixedWriter.SetBoundary(first70("MIXED-" + mixedWriter.Boundary()))
relatedWriter, alternativeBoundary, err := nestedMultipart(mixedWriter, "multipart/related", relatedBoundary)
if err != nil {
return err
}
altWriter, _, err := nestedMultipart(relatedWriter, "multipart/alternative", "ALTERNATIVE-"+alternativeBoundary)
if err != nil {
return err
}
if email.Text != "" {
childContent, err := altWriter.CreatePart(textproto.MIMEHeader{
"Content-Type": {"text/plain; charset=\"UTF-8\""},
"Content-Transfer-Encoding": {"quoted-printable"},
"Content-Disposition": {"inline"},
})
if err != nil {
return err
}
enc := quotedprintable.NewWriter(childContent)
enc.Write([]byte(email.Text + "\r\n\r\n"))
}
if email.HTML != "" {
childContent, err := altWriter.CreatePart(textproto.MIMEHeader{
"Content-Type": {"text/html; charset=\"UTF-8\""},
"Content-Transfer-Encoding": {"quoted-printable"},
"Content-Disposition": {"inline"},
})
if err != nil {
return err
}
enc := quotedprintable.NewWriter(childContent)
enc.Write([]byte(email.HTML + "\r\n\r\n"))
}
if err := altWriter.Close(); err != nil {
return err
}
if err := relatedWriter.Close(); err != nil {
return err
}
dest.Write([]byte("From: " + email.From + "\r\n"))
dest.Write([]byte("To: " + email.To + "\r\n"))
if email.ReturnPath != "" {
if string(email.ReturnPath[0]) == "<" {
dest.Write([]byte("Return-Path: " + email.ReturnPath + "\r\n"))
} else {
dest.Write([]byte("Return-Path: <" + email.ReturnPath + ">\r\n"))
}
}
if email.ReplyTo != "" {
dest.Write([]byte("Reply-To: " + email.ReplyTo + "\r\n"))
}
subjectHeader, err := encodeHeader("Subject", email.Subject)
if err != nil {
return err
}
dest.Write([]byte(subjectHeader))
dest.Write([]byte("Date: " + time.Now().Format(time.RFC1123Z) + "\r\n"))
dest.Write([]byte("MIME-Version: 1.0\r\n"))
// Attachments
for _, attachment := range email.Attachments {
fileContent, err := mixedWriter.CreatePart(textproto.MIMEHeader{
"Content-Type": {
attachment.ContentType + "; name=\"" + attachment.Filename + "\"",
}, "Content-Disposition": {
"attachment; filename=\"" + attachment.Filename + "\"",
}, "Content-Transfer-Encoding": {
"base64",
},
})
if err != nil {
return err
}
enc := base64.NewEncoder(base64.StdEncoding, fileContent)
if _, err := io.Copy(enc, attachment.Data); err != nil {
return err
}
if err := enc.Close(); err != nil {
return err
}
fileContent.Write([]byte("\r\n\r\n"))
}
dest.Write([]byte("Content-Type: multipart/mixed; boundary="))
dest.Write([]byte(`"` + mixedWriter.Boundary() + "\"\r\n\r\n"))
if _, err := io.Copy(dest, mixedContent); err != nil {
return err
}
dest.Write([]byte("--" + mixedWriter.Boundary() + "--\r\n\r\n"))
if err := mixedWriter.Close(); err != nil {
return err
}
return nil
}
func nestedMultipart(enclosingWriter *multipart.Writer, contentType, boundary string) (*multipart.Writer, string, error) {
var nestedWriter *multipart.Writer
var newBoundary string
boundary = first70(boundary)
contentWithBoundary := contentType + "; boundary=\"" + boundary + "\""
if contentType == "multipart/related" {
contentWithBoundary += "; type=\"Text/HTML\""
}
contentBuffer, err := enclosingWriter.CreatePart(textproto.MIMEHeader{"Content-Type": {contentWithBoundary}})
if err != nil {
return nestedWriter, newBoundary, err
}
nestedWriter = multipart.NewWriter(contentBuffer)
newBoundary = nestedWriter.Boundary()
nestedWriter.SetBoundary(boundary)
return nestedWriter, newBoundary, nil
}
func first70(str string) string {
if len(str) > 70 {
return string(str[0:69])
}
return str
}
func isASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
return false
}
}
return true
}
func encodeHeader(name, content string) (string, error) {
if isASCII(content) {
if len(content) < 66 {
return fmt.Sprintf("%s: %s\r\n", name, content), nil
}
}
encoded, err := encodeWord(content)
if err != nil {
return "", err
}
headerLine := fmt.Sprintf("%s: =?utf-8?Q?%s?=", name, encoded)
if len(headerLine) < 75 {
return headerLine + "\r\n", nil
}
var ret string
// Chunk the header in 48 character segments. The maximum length is 75 including the "Subject: " and other encoding line noise.
// We could go 48 on the first and 59 on subsequent lines but it's just not worth the effort
chunks := chunkString(content, 48)
for i, chunk := range chunks {
encoded, err := encodeWord(chunk)
if err != nil {
return "", err
}
if i == 0 {
ret += fmt.Sprintf("%s: =?utf-8?Q?%s?=\r\n ", name, encoded)
} else if i+1 == len(chunks) {
// For the last chunk, no trailing space
ret += fmt.Sprintf("=?utf-8?Q?%s?=\r\n", encoded)
} else {
// Multi-line headers are denoted by the CLRF followed by a SPACE
ret += fmt.Sprintf("=?utf-8?Q?%s?=\r\n ", encoded)
}
}
return ret, nil
}
func encodeWord(str string) (string, error) {
var buf bytes.Buffer
writer := quotedprintable.NewWriter(&buf)
if _, err := writer.Write([]byte(str)); err != nil {
return "", err
}
if err := writer.Close(); err != nil {
return "", err
}
return buf.String(), nil
}
func chunkString(s string, chunkSize int) []string {
if len(s) == 0 {
return nil
}
if chunkSize >= len(s) {
return []string{s}
}
var chunks []string = make([]string, 0, (len(s)-1)/chunkSize+1)
currentLen := 0
currentStart := 0
for i := range s {
if currentLen == chunkSize {
chunks = append(chunks, s[currentStart:i])
currentLen = 0
currentStart = i
}
currentLen++
}
chunks = append(chunks, s[currentStart:])
return chunks
}