forked from sorbits/go.enmime
/
mail.go
213 lines (192 loc) · 5.95 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
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
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
OtherParts []MIMEPart // All parts not in Attachments and Inlines
header mail.Header // Header from original message
}
var AddressHeaders = []string{"From", "To", "Delivered-To", "Cc", "Bcc", "Reply-To"}
// 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",
"multipart/signed":
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 := &MIMEBody{header: mailMsg.Header}
if !IsMultipartMessage(mailMsg) {
// Parse as text only
bodyBytes, err := decodeSection(mailMsg.Header.Get("Content-Transfer-Encoding"),
mailMsg.Body)
if err != nil {
return nil, fmt.Errorf("Error decoding text-only message: %v", err)
}
mimeMsg.Text = string(bodyBytes)
// Check for HTML at top-level, eat errors quietly
ctype := mailMsg.Header.Get("Content-Type")
if ctype != "" {
if mediatype, mparams, err := mime.ParseMediaType(ctype); err == nil {
/*
*Content-Type: text/plain;\t charset="hz-gb-2312"
*/
if mparams["charset"] != "" {
newStr, err := ConvertToUTF8String(mparams["charset"], mimeMsg.Text)
if err != nil {
return nil, err
}
mimeMsg.Text = newStr
}
if mediatype == "text/html" {
mimeMsg.Html = mimeMsg.Text
}
}
}
} else {
// Parse top-level multipart
ctype := mailMsg.Header.Get("Content-Type")
mediatype, params, err := mime.ParseMediaType(ctype)
if err != nil {
return nil, fmt.Errorf("Unable to parse media type: %v", 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
if mediatype == "multipart/altern" {
match := BreadthMatchFirst(root, func(p MIMEPart) bool {
return p.ContentType() == "text/plain" && p.Disposition() != "attachment"
})
if match != nil {
if match.Charset() != "" {
newStr, err := ConvertToUTF8String(match.Charset(), string(match.Content()))
if err != nil {
return nil, err
}
mimeMsg.Text += newStr
} else {
mimeMsg.Text += string(match.Content())
}
}
} else {
// multipart is of a mixed type
match := DepthMatchAll(root, func(p MIMEPart) bool {
return p.ContentType() == "text/plain" && p.Disposition() != "attachment"
})
for i, m := range match {
if i > 0 {
mimeMsg.Text += "\n--\n"
}
if m.Charset() != "" {
newStr, err := ConvertToUTF8String(m.Charset(), string(m.Content()))
if err != nil {
return nil, err
}
mimeMsg.Text += newStr
} else {
mimeMsg.Text += string(m.Content())
}
}
}
// Locate HTML body
match := BreadthMatchFirst(root, func(p MIMEPart) bool {
return p.ContentType() == "text/html" && p.Disposition() != "attachment"
})
if match != nil {
if match.Charset() != "" {
newStr, err := ConvertToUTF8String(match.Charset(), string(match.Content()))
if err != nil {
return nil, err
}
mimeMsg.Html += newStr
} else {
mimeMsg.Html = string(match.Content())
}
}
// Locate attachments
mimeMsg.Attachments = BreadthMatchAll(root, func(p MIMEPart) bool {
return p.Disposition() == "attachment" || p.ContentType() == "application/octet-stream"
})
// Locate inlines
mimeMsg.Inlines = BreadthMatchAll(root, func(p MIMEPart) bool {
return p.Disposition() == "inline"
})
// Locate others parts not handled in "Attachments" and "inlines"
mimeMsg.OtherParts = BreadthMatchAll(root, func(p MIMEPart) bool {
if strings.HasPrefix(p.ContentType(), "multipart/") {
return false
}
if p.Disposition() != "" {
return false
}
if p.ContentType() == "application/octet-stream" {
return false
}
return p.ContentType() != "text/plain" && p.ContentType() != "text/html"
})
}
return mimeMsg, nil
}
// Process the specified header for RFC 2047 encoded words and return the result
func (m *MIMEBody) GetHeader(name string) string {
return DecodeHeader(m.header.Get(name))
}
// Return AddressList with RFC 2047 encoded encoded names.
func (m *MIMEBody) AddressList(key string) ([]*mail.Address, error) {
isAddrHeader := false
for _, hkey := range AddressHeaders {
if strings.ToLower(hkey) == strings.ToLower(key) {
isAddrHeader = true
break
}
}
if !isAddrHeader {
return nil, fmt.Errorf("%s is not address header", key)
}
str := DecodeToUTF8Base64Header(m.header.Get(key))
if str == "" {
return nil, mail.ErrHeaderNotPresent
}
ret, err := mail.ParseAddressList(str)
if err != nil {
return nil, err
}
return ret, nil
}