Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 1 | package parsemail |
| 2 | |
| 3 | import ( |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 4 | "bytes" |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 5 | "encoding/base64" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "io/ioutil" |
| 10 | "mime" |
| 11 | "mime/multipart" |
| 12 | "net/mail" |
| 13 | "strings" |
| 14 | "time" |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 15 | ) |
| 16 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 17 | const content_type_multipart_mixed = "multipart/mixed" |
| 18 | const content_type_multipart_alternative = "multipart/alternative" |
| 19 | const content_type_multipart_related = "multipart/related" |
| 20 | const content_type_text_html = "text/html" |
| 21 | const content_type_text_plain = "text/plain" |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 22 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 23 | func Parse(r io.Reader) (email Email, err error) { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 24 | msg, err := mail.ReadMessage(r) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 25 | if err != nil { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 26 | return |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 27 | } |
| 28 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 29 | email, err = createEmailFromHeader(msg.Header) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 30 | if err != nil { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 31 | return |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 34 | contentType, params, err := parseContentType(msg.Header.Get("Content-Type")) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 35 | if err != nil { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 36 | return |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 39 | switch contentType { |
| 40 | case content_type_multipart_mixed: |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 41 | email.TextBody, email.HTMLBody, email.Attachments, email.EmbeddedFiles, err = parseMultipartMixed(msg.Body, params["boundary"]) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 42 | case content_type_multipart_alternative: |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 43 | email.TextBody, email.HTMLBody, email.EmbeddedFiles, err = parseMultipartAlternative(msg.Body, params["boundary"]) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 44 | case content_type_text_plain: |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 45 | message, _ := ioutil.ReadAll(msg.Body) |
| 46 | email.TextBody = strings.TrimSuffix(string(message[:]), "\n") |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 47 | case content_type_text_html: |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 48 | message, _ := ioutil.ReadAll(msg.Body) |
| 49 | email.HTMLBody = strings.TrimSuffix(string(message[:]), "\n") |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 50 | case "": |
| 51 | err = errors.New("No top level mime type specified") |
| 52 | default: |
| 53 | err = errors.New(fmt.Sprintf("Unknown top level mime type: %s", contentType)) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 56 | return |
| 57 | } |
| 58 | |
| 59 | func createEmailFromHeader(header mail.Header) (email Email, err error) { |
| 60 | email.Subject = header.Get("Subject") |
| 61 | |
| 62 | email.From, err = parseAddressList(header.Get("From")) |
| 63 | if err != nil { |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | email.Sender, err = parseAddress(header.Get("Sender")) |
| 68 | if err != nil { |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | email.ReplyTo, err = parseAddressList(header.Get("Reply-To")) |
| 73 | if err != nil { |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | email.To, err = parseAddressList(header.Get("To")) |
| 78 | if err != nil { |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | email.Cc, err = parseAddressList(header.Get("Cc")) |
| 83 | if err != nil { |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | email.Bcc, err = parseAddressList(header.Get("Bcc")) |
| 88 | if err != nil { |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | email.Date, err = parseTime(header.Get("Date")) |
| 93 | if err != nil { |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | email.ResentFrom, err = parseAddressList(header.Get("Resent-From")) |
| 98 | if err != nil { |
| 99 | return |
| 100 | } |
| 101 | |
| 102 | email.ResentSender, err = parseAddress(header.Get("Resent-Sender")) |
| 103 | if err != nil { |
| 104 | return |
| 105 | } |
| 106 | |
| 107 | email.ResentTo, err = parseAddressList(header.Get("Resent-To")) |
| 108 | if err != nil { |
| 109 | return |
| 110 | } |
| 111 | |
| 112 | email.ResentCc, err = parseAddressList(header.Get("Resent-Cc")) |
| 113 | if err != nil { |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | email.ResentBcc, err = parseAddressList(header.Get("Resent-Bcc")) |
| 118 | if err != nil { |
| 119 | return |
| 120 | } |
| 121 | |
| 122 | if header.Get("Resent-Date") == "" { |
| 123 | email.ResentDate = time.Time{} |
| 124 | } else { |
| 125 | email.ResentDate, err = parseTime(header.Get("Resent-Date")) |
| 126 | if err != nil { |
| 127 | return |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | email.ResentMessageID = parseMessageId(header.Get("Resent-Message-ID")) |
| 132 | email.MessageID = parseMessageId(header.Get("Message-ID")) |
| 133 | email.InReplyTo = parseMessageIdList(header.Get("In-Reply-To")) |
| 134 | email.References = parseMessageIdList(header.Get("References")) |
| 135 | |
| 136 | //decode whole header for easier access to extra fields |
| 137 | //todo: should we decode? aren't only standard fields mime encoded? |
| 138 | email.Header, err = decodeHeaderMime(header) |
| 139 | if err != nil { |
| 140 | return |
| 141 | } |
| 142 | |
| 143 | return |
| 144 | } |
| 145 | |
| 146 | func parseContentType(contentTypeHeader string) (contentType string, params map[string]string, err error) { |
| 147 | if contentTypeHeader == "" { |
| 148 | contentType = content_type_text_plain |
| 149 | return |
| 150 | } |
| 151 | |
| 152 | return mime.ParseMediaType(contentTypeHeader) |
| 153 | } |
| 154 | |
| 155 | func parseAddress(s string) (*mail.Address, error) { |
| 156 | if strings.Trim(s, " \n") != "" { |
| 157 | return mail.ParseAddress(s) |
| 158 | } |
| 159 | |
| 160 | return nil, nil |
| 161 | } |
| 162 | |
| 163 | func parseAddressList(s string) ([]*mail.Address, error) { |
| 164 | if strings.Trim(s, " \n") != "" { |
| 165 | return mail.ParseAddressList(s) |
| 166 | } |
| 167 | |
| 168 | return []*mail.Address{}, nil |
| 169 | } |
| 170 | |
| 171 | func parseTime(s string) (time.Time, error) { |
| 172 | t, err := time.Parse(time.RFC1123Z, s) |
| 173 | if err == nil { |
| 174 | return t, err |
| 175 | } |
| 176 | |
| 177 | return time.Parse("Mon, 2 Jan 2006 15:04:05 -0700", s) |
| 178 | } |
| 179 | |
| 180 | func parseMessageId(s string) string { |
| 181 | return strings.Trim(s, "<> ") |
| 182 | } |
| 183 | |
| 184 | func parseMessageIdList(s string) (result []string) { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 185 | for _, p := range strings.Split(s, " ") { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 186 | if strings.Trim(p, " \n") != "" { |
| 187 | result = append(result, parseMessageId(p)) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return |
| 192 | } |
| 193 | |
| 194 | func parseMultipartAlternative(msg io.Reader, boundary string) (textBody, htmlBody string, embeddedFiles []EmbeddedFile, err error) { |
| 195 | pmr := multipart.NewReader(msg, boundary) |
| 196 | for { |
| 197 | part, err := pmr.NextPart() |
| 198 | |
| 199 | if err == io.EOF { |
| 200 | break |
| 201 | } else if err != nil { |
| 202 | return textBody, htmlBody, embeddedFiles, err |
| 203 | } |
| 204 | |
| 205 | contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type")) |
| 206 | |
| 207 | switch contentType { |
| 208 | case content_type_text_plain: |
| 209 | ppContent, err := ioutil.ReadAll(part) |
| 210 | if err != nil { |
| 211 | return textBody, htmlBody, embeddedFiles, err |
| 212 | } |
| 213 | |
| 214 | textBody += strings.TrimSuffix(string(ppContent[:]), "\n") |
| 215 | case content_type_text_html: |
| 216 | ppContent, err := ioutil.ReadAll(part) |
| 217 | if err != nil { |
| 218 | return textBody, htmlBody, embeddedFiles, err |
| 219 | } |
| 220 | |
| 221 | htmlBody += strings.TrimSuffix(string(ppContent[:]), "\n") |
| 222 | case content_type_multipart_related: |
| 223 | var tb, hb string |
| 224 | var ef []EmbeddedFile |
| 225 | tb, hb, ef, err = parseMultipartAlternative(part, params["boundary"]) |
| 226 | htmlBody += hb |
| 227 | textBody += tb |
| 228 | embeddedFiles = append(embeddedFiles, ef...) |
| 229 | default: |
| 230 | if isEmbeddedFile(part) { |
| 231 | ef, err := decodeEmbeddedFile(part) |
| 232 | if err != nil { |
| 233 | return textBody, htmlBody, embeddedFiles, err |
| 234 | } |
| 235 | |
| 236 | embeddedFiles = append(embeddedFiles, ef) |
| 237 | } else { |
| 238 | return textBody, htmlBody, embeddedFiles, errors.New(fmt.Sprintf("Can't process multipart/alternative inner mime type: %s", contentType)) |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return textBody, htmlBody, embeddedFiles, err |
| 244 | } |
| 245 | |
| 246 | func parseMultipartMixed(msg io.Reader, boundary string) (textBody, htmlBody string, attachments []Attachment, embeddedFiles []EmbeddedFile, err error) { |
| 247 | mr := multipart.NewReader(msg, boundary) |
| 248 | for { |
| 249 | part, err := mr.NextPart() |
| 250 | if err == io.EOF { |
| 251 | break |
| 252 | } else if err != nil { |
| 253 | return textBody, htmlBody, attachments, embeddedFiles, err |
| 254 | } |
| 255 | |
| 256 | contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type")) |
| 257 | if err != nil { |
| 258 | return textBody, htmlBody, attachments, embeddedFiles, err |
| 259 | } |
| 260 | |
| 261 | if contentType == content_type_multipart_alternative { |
| 262 | textBody, htmlBody, embeddedFiles, err = parseMultipartAlternative(part, params["boundary"]) |
| 263 | if err != nil { |
| 264 | return textBody, htmlBody, attachments, embeddedFiles, err |
| 265 | } |
| 266 | } else if isAttachment(part) { |
| 267 | at, err := decodeAttachment(part) |
| 268 | if err != nil { |
| 269 | return textBody, htmlBody, attachments, embeddedFiles, err |
| 270 | } |
| 271 | |
| 272 | attachments = append(attachments, at) |
| 273 | } else { |
| 274 | return textBody, htmlBody, attachments, embeddedFiles, errors.New(fmt.Sprintf("Unknown multipart/mixed nested mime type: %s", contentType)) |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return textBody, htmlBody, attachments, embeddedFiles, err |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | func decodeMimeSentence(s string) (string, error) { |
| 282 | result := []string{} |
| 283 | ss := strings.Split(s, " ") |
| 284 | |
| 285 | for _, word := range ss { |
| 286 | dec := new(mime.WordDecoder) |
| 287 | w, err := dec.Decode(word) |
| 288 | if err != nil { |
| 289 | if len(result) == 0 { |
| 290 | w = word |
| 291 | } else { |
| 292 | w = " " + word |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | result = append(result, w) |
| 297 | } |
| 298 | |
| 299 | return strings.Join(result, ""), nil |
| 300 | } |
| 301 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 302 | func decodeHeaderMime(header mail.Header) (mail.Header, error) { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 303 | parsedHeader := map[string][]string{} |
| 304 | |
| 305 | for headerName, headerData := range header { |
| 306 | |
| 307 | parsedHeaderData := []string{} |
| 308 | for _, headerValue := range headerData { |
| 309 | decodedHeaderValue, err := decodeMimeSentence(headerValue) |
| 310 | if err != nil { |
| 311 | return mail.Header{}, err |
| 312 | } |
| 313 | parsedHeaderData = append(parsedHeaderData, decodedHeaderValue) |
| 314 | } |
| 315 | |
| 316 | parsedHeader[headerName] = parsedHeaderData |
| 317 | } |
| 318 | |
| 319 | return mail.Header(parsedHeader), nil |
| 320 | } |
| 321 | |
| 322 | func decodePartData(part *multipart.Part) (io.Reader, error) { |
| 323 | encoding := part.Header.Get("Content-Transfer-Encoding") |
| 324 | |
| 325 | if encoding == "base64" { |
| 326 | dr := base64.NewDecoder(base64.StdEncoding, part) |
| 327 | dd, err := ioutil.ReadAll(dr) |
| 328 | if err != nil { |
| 329 | return nil, err |
| 330 | } |
| 331 | |
| 332 | return bytes.NewReader(dd), nil |
| 333 | } else { |
| 334 | return nil, errors.New(fmt.Sprintf("Unknown encoding: %s", encoding)) |
| 335 | } |
| 336 | } |
| 337 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 338 | func isEmbeddedFile(part *multipart.Part) bool { |
| 339 | return part.Header.Get("Content-Transfer-Encoding") != "" |
| 340 | } |
| 341 | |
| 342 | func decodeEmbeddedFile(part *multipart.Part) (ef EmbeddedFile, err error) { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 343 | cid, err := decodeMimeSentence(part.Header.Get("Content-Id")) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 344 | if err != nil { |
| 345 | return |
| 346 | } |
| 347 | |
| 348 | decoded, err := decodePartData(part) |
| 349 | if err != nil { |
| 350 | return |
| 351 | } |
| 352 | |
| 353 | ef.CID = strings.Trim(cid, "<>") |
| 354 | ef.Data = decoded |
| 355 | ef.ContentType = part.Header.Get("Content-Type") |
| 356 | |
| 357 | return |
| 358 | } |
| 359 | |
| 360 | func isAttachment(part *multipart.Part) bool { |
| 361 | return part.FileName() != "" |
| 362 | } |
| 363 | |
| 364 | func decodeAttachment(part *multipart.Part) (at Attachment, err error) { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 365 | filename, err := decodeMimeSentence(part.FileName()) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 366 | if err != nil { |
| 367 | return |
| 368 | } |
| 369 | |
| 370 | decoded, err := decodePartData(part) |
| 371 | if err != nil { |
| 372 | return |
| 373 | } |
| 374 | |
| 375 | at.Filename = filename |
| 376 | at.Data = decoded |
| 377 | at.ContentType = strings.Split(part.Header.Get("Content-Type"), ";")[0] |
| 378 | |
| 379 | return |
| 380 | } |
| 381 | |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 382 | type Attachment struct { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 383 | Filename string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 384 | ContentType string |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 385 | Data io.Reader |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | type EmbeddedFile struct { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 389 | CID string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 390 | ContentType string |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 391 | Data io.Reader |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 394 | type MessageID string |
| 395 | |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 396 | type Email struct { |
| 397 | Header mail.Header |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 398 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 399 | Subject string |
| 400 | Sender *mail.Address |
| 401 | From []*mail.Address |
| 402 | ReplyTo []*mail.Address |
| 403 | To []*mail.Address |
| 404 | Cc []*mail.Address |
| 405 | Bcc []*mail.Address |
| 406 | Date time.Time |
| 407 | MessageID string |
| 408 | InReplyTo []string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 409 | References []string |
| 410 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 411 | ResentFrom []*mail.Address |
| 412 | ResentSender *mail.Address |
| 413 | ResentTo []*mail.Address |
| 414 | ResentDate time.Time |
| 415 | ResentCc []*mail.Address |
| 416 | ResentBcc []*mail.Address |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 417 | ResentMessageID string |
| 418 | |
| 419 | Received string |
| 420 | |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 421 | HTMLBody string |
| 422 | TextBody string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 423 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 424 | Attachments []Attachment |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 425 | EmbeddedFiles []EmbeddedFile |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame^] | 426 | } |