forked from jhillyerd/enmime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envelope.go
354 lines (318 loc) · 10.2 KB
/
envelope.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
package enmime
import (
"fmt"
"io"
"io/ioutil"
"net/mail"
"net/textproto"
"strings"
"github.com/jaytaylor/html2text"
)
// Envelope is a simplified wrapper for MIME email messages.
type Envelope struct {
Text string // The plain text portion of the message
HTML string // The HTML portion of the message
Root *Part // The top-level Part
Attachments []*Part // All parts having a Content-Disposition of attachment
Inlines []*Part // All parts having a Content-Disposition of inline
OtherParts []*Part // All parts not in Attachments and Inlines
Errors []*Error // Errors encountered while parsing
header *textproto.MIMEHeader // Header from original message
}
// GetHeader processes the specified header for RFC 2047 encoded words and returns the result as a
// UTF-8 string
func (e *Envelope) GetHeader(name string) string {
if e.header == nil {
return ""
}
return decodeHeader(e.header.Get(name))
}
// AddressList returns a mail.Address slice with RFC 2047 encoded names converted to UTF-8
func (e *Envelope) AddressList(key string) ([]*mail.Address, error) {
if e.header == nil {
return nil, fmt.Errorf("No headers available")
}
if !AddressHeaders[strings.ToLower(key)] {
return nil, fmt.Errorf("%s is not an address header", key)
}
str := decodeToUTF8Base64Header(e.header.Get(key))
if str == "" {
return nil, mail.ErrHeaderNotPresent
}
// These statements are handy for debugging ParseAddressList errors
// fmt.Println("in: ", m.header.Get(key))
// fmt.Println("out: ", str)
ret, err := mail.ParseAddressList(str)
if err != nil {
return nil, err
}
return ret, nil
}
// ReadEnvelope is a wrapper around ReadParts and EnvelopeFromPart. It parses the content of the
// provided reader into an Envelope, downconverting HTML to plain text if needed, and sorting the
// attachments, inlines and other parts into their respective slices. Errors are collected from all
// Parts and placed into the Envelope.Errors slice.
func ReadEnvelope(r io.Reader) (*Envelope, error) {
// Read MIME parts from reader
root, err := ReadParts(r)
if err != nil {
return nil, fmt.Errorf("Failed to ReadParts: %v", err)
}
return EnvelopeFromPart(root)
}
// EnvelopeFromPart uses the provided Part tree to build an Envelope, downconverting HTML to plain
// text if needed, and sorting the attachments, inlines and other parts into their respective
// slices. Errors are collected from all Parts and placed into the Envelopes Errors slice.
func EnvelopeFromPart(root *Part) (*Envelope, error) {
e := &Envelope{
Root: root,
header: &root.Header,
}
if isMultipartMessage(root) {
// Multi-part message (message with attachments, etc)
if err := parseMultiPartBody(root, e); err != nil {
return nil, err
}
} else {
if isBinaryBody(root) {
// Attachment only, no text
if err := parseBinaryOnlyBody(root, e); err != nil {
return nil, err
}
} else {
// Only text, no attachments
if err := parseTextOnlyBody(root, e); err != nil {
return nil, err
}
}
}
// Down-convert HTML to text if necessary
if e.Text == "" && e.HTML != "" {
// We always warn when this happens
e.Root.addWarning(
errorPlainTextFromHTML,
"Message did not contain a text/plain part")
var err error
if e.Text, err = html2text.FromString(e.HTML); err != nil {
// Downcoversion shouldn't fail
e.Text = ""
p := e.Root.BreadthMatchFirst(matchHTMLBodyPart)
p.addError(
errorPlainTextFromHTML,
"Failed to downconvert HTML: %v",
err)
}
}
// Copy part errors into Envelope
if e.Root != nil {
_ = e.Root.DepthMatchAll(func(part *Part) bool {
// Using DepthMatchAll to traverse all parts, don't care about result
for i := range part.Errors {
// Index is required here to get the correct address, &value from range
// points to a locally scoped variable
e.Errors = append(e.Errors, &part.Errors[i])
}
return false
})
}
return e, nil
}
// parseTextOnlyBody parses a plain text message in root that has MIME-like headers, but
// only contains a single part - no boundaries, etc. The result is placed in e.
func parseTextOnlyBody(root *Part, e *Envelope) error {
// Determine character set
var charset string
var isHTML bool
if ctype := root.Header.Get(hnContentType); ctype != "" {
if mediatype, mparams, err := parseMediaType(ctype); err == nil {
isHTML = (mediatype == ctTextHTML)
if mparams[hpCharset] != "" {
charset = mparams[hpCharset]
}
}
}
// Read transcoded text
bodyBytes, err := ioutil.ReadAll(root)
if err != nil {
return err
}
if isHTML {
rawHTML := string(bodyBytes)
// Note: Empty e.Text will trigger html2text conversion
e.HTML = rawHTML
if charset == "" {
// Search for charset in HTML metadata
if charset = findCharsetInHTML(rawHTML); charset != "" {
// Found charset in HTML
if convHTML, err := convertToUTF8String(charset, bodyBytes); err == nil {
// Successful conversion
e.HTML = convHTML
} else {
// Conversion failed
root.addWarning(errorCharsetConversion, err.Error())
}
}
}
} else {
e.Text = string(bodyBytes)
}
return nil
}
// parseBinaryOnlyBody parses a message where the only content is a binary attachment with no
// other parts. The result is placed in e.
func parseBinaryOnlyBody(root *Part, e *Envelope) error {
// Determine mediatype
ctype := root.Header.Get(hnContentType)
mediatype, mparams, err := parseMediaType(ctype)
if err != nil {
mediatype = cdAttachment
}
// Determine and set headers for: content disposition, filename and character set
root.setupContentHeaders(mparams)
// Add our part to the appropriate section of the Envelope
e.Root = NewPart(nil, mediatype)
if root.Disposition == cdInline {
e.Inlines = append(e.Inlines, root)
} else {
e.Attachments = append(e.Attachments, root)
}
return nil
}
// parseMultiPartBody parses a multipart message in root. The result is placed in e.
func parseMultiPartBody(root *Part, e *Envelope) error {
// Parse top-level multipart
ctype := root.Header.Get(hnContentType)
mediatype, params, err := parseMediaType(ctype)
if err != nil {
return fmt.Errorf("Unable to parse media type: %v", err)
}
if !strings.HasPrefix(mediatype, ctMultipartPrefix) {
return fmt.Errorf("Unknown mediatype: %v", mediatype)
}
boundary := params[hpBoundary]
if boundary == "" {
return fmt.Errorf("Unable to locate boundary param in Content-Type header")
}
// Locate text body
if mediatype == ctMultipartAltern {
p := root.BreadthMatchFirst(func(p *Part) bool {
return p.ContentType == ctTextPlain && p.Disposition != cdAttachment
})
if p != nil {
allBytes, ioerr := ioutil.ReadAll(p)
if ioerr != nil {
return ioerr
}
e.Text = string(allBytes)
}
} else {
// multipart is of a mixed type
parts := root.DepthMatchAll(func(p *Part) bool {
return p.ContentType == ctTextPlain && p.Disposition != cdAttachment
})
for i, p := range parts {
if i > 0 {
e.Text += "\n--\n"
}
allBytes, ioerr := ioutil.ReadAll(p)
if ioerr != nil {
return ioerr
}
e.Text += string(allBytes)
}
}
// Locate HTML body
p := root.BreadthMatchFirst(matchHTMLBodyPart)
if p != nil {
allBytes, ioerr := ioutil.ReadAll(p)
if ioerr != nil {
return ioerr
}
e.HTML += string(allBytes)
}
// Locate attachments
e.Attachments = root.BreadthMatchAll(func(p *Part) bool {
return p.Disposition == cdAttachment || p.ContentType == ctAppOctetStream
})
// Locate inlines
e.Inlines = root.BreadthMatchAll(func(p *Part) bool {
return p.Disposition == cdInline
})
// Locate others parts not considered in attachments or inlines
e.OtherParts = root.BreadthMatchAll(func(p *Part) bool {
if strings.HasPrefix(p.ContentType, ctMultipartPrefix) {
return false
}
if p.Disposition != "" {
return false
}
if p.ContentType == ctAppOctetStream {
return false
}
return p.ContentType != ctTextPlain && p.ContentType != ctTextHTML
})
return nil
}
// isMultipartMessage returns true if the message has a recognized multipart Content-Type header.
func isMultipartMessage(root *Part) bool {
// Parse top-level multipart
ctype := root.Header.Get(hnContentType)
mediatype, _, err := parseMediaType(ctype)
if err != nil {
return false
}
// According to rfc2046#section-5.1.7 all other multipart should
// be treated as multipart/mixed
return strings.HasPrefix(mediatype, ctMultipartPrefix)
}
// isAttachment returns true, if the given header defines an attachment. First it checks if the
// Content-Disposition header defines an attachement or inline attachment. If this test is false,
// the Content-Type header is checked for attachment, but not inline. Email clients use inline for
// their text bodies.
//
// Valid Attachment-Headers:
//
// - Content-Disposition: attachment; filename="frog.jpg"
// - Content-Disposition: inline; filename="frog.jpg"
// - Content-Type: attachment; filename="frog.jpg"
func isAttachment(header textproto.MIMEHeader) bool {
mediatype, _, _ := parseMediaType(header.Get(hnContentDisposition))
if strings.ToLower(mediatype) == cdAttachment ||
strings.ToLower(mediatype) == cdInline {
return true
}
mediatype, _, _ = parseMediaType(header.Get(hnContentType))
if strings.ToLower(mediatype) == cdAttachment {
return true
}
return false
}
// isPlain returns true, if the the MIME headers define a valid 'text/plain' or 'text/html' part. If
// the emptyContentTypeIsPlain argument is set to true, a missing Content-Type header will result in
// a positive plain part detection.
func isPlain(header textproto.MIMEHeader, emptyContentTypeIsPlain bool) bool {
ctype := header.Get(hnContentType)
if ctype == "" && emptyContentTypeIsPlain {
return true
}
mediatype, _, err := parseMediaType(ctype)
if err != nil {
return false
}
switch mediatype {
case ctTextPlain, ctTextHTML:
return true
}
return false
}
// isBinaryBody returns true if the mail header defines a binary body.
func isBinaryBody(root *Part) bool {
if isPlain(root.Header, true) {
return false
}
return isAttachment(root.Header)
}
// Used by Part matchers to locate the HTML body. Not inlined because it's used in multiple places.
func matchHTMLBodyPart(p *Part) bool {
return p.ContentType == ctTextHTML && p.Disposition != cdAttachment
}