-
Notifications
You must be signed in to change notification settings - Fork 23
/
mail.go
105 lines (93 loc) · 2.93 KB
/
mail.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
package enmime
import (
"fmt"
"mime"
"net/mail"
"strings"
)
// MIMEBody is the outer wrapper for MIME messages.
type MIMEBody struct {
Text string // The plain text portion of the message
Html string // The HTML portion of the message
Root MIMEPart // The top-level MIMEPart
Attachments []MIMEPart // All parts having a Content-Disposition of attachment
Inlines []MIMEPart // All parts having a Content-Disposition of inline
}
// IsMultipartMessage returns true if the message has a recognized multipart Content-Type
// header. You don't need to check this before calling ParseMIMEBody, it can handle
// non-multipart messages.
func IsMultipartMessage(mailMsg *mail.Message) bool {
// Parse top-level multipart
ctype := mailMsg.Header.Get("Content-Type")
mediatype, _, err := mime.ParseMediaType(ctype)
if err != nil {
return false
}
switch mediatype {
case "multipart/alternative",
"multipart/mixed",
"multipart/related":
return true
}
return false
}
// ParseMIMEBody parses the body of the message object into a tree of MIMEPart objects,
// each of which is aware of its content type, filename and headers. If the part was
// encoded in quoted-printable or base64, it is decoded before being stored in the
// MIMEPart object.
func ParseMIMEBody(mailMsg *mail.Message) (*MIMEBody, error) {
mimeMsg := new(MIMEBody)
if !IsMultipartMessage(mailMsg) {
// Parse as text only
bodyBytes, err := decodeSection(mailMsg.Header.Get("Content-Transfer-Encoding"),
mailMsg.Body)
if err != nil {
return nil, err
}
mimeMsg.Text = string(bodyBytes)
} else {
// Parse top-level multipart
ctype := mailMsg.Header.Get("Content-Type")
mediatype, params, err := mime.ParseMediaType(ctype)
if err != nil {
return nil, err
}
if !strings.HasPrefix(mediatype, "multipart/") {
return nil, fmt.Errorf("Unknown mediatype: %v", mediatype)
}
boundary := params["boundary"]
if boundary == "" {
return nil, fmt.Errorf("Unable to locate boundary param in Content-Type header")
}
// Root Node of our tree
root := NewMIMEPart(nil, mediatype)
mimeMsg.Root = root
err = parseParts(root, mailMsg.Body, boundary)
if err != nil {
return nil, err
}
// Locate text body
match := BreadthMatchFirst(root, func(p MIMEPart) bool {
return p.ContentType() == "text/plain" && p.Disposition() != "attachment"
})
if match != nil {
mimeMsg.Text = string(match.Content())
}
// Locate HTML body
match = BreadthMatchFirst(root, func(p MIMEPart) bool {
return p.ContentType() == "text/html" && p.Disposition() != "attachment"
})
if match != nil {
mimeMsg.Html = string(match.Content())
}
// Locate attachments
mimeMsg.Attachments = BreadthMatchAll(root, func(p MIMEPart) bool {
return p.Disposition() == "attachment"
})
// Locate inlines
mimeMsg.Inlines = BreadthMatchAll(root, func(p MIMEPart) bool {
return p.Disposition() == "inline"
})
}
return mimeMsg, nil
}