| package parsemail |
| |
| import ( |
| "bytes" |
| "encoding/base64" |
| "errors" |
| "fmt" |
| "io" |
| "io/ioutil" |
| "mime" |
| "mime/multipart" |
| "net/mail" |
| "strings" |
| "time" |
| ) |
| |
| const content_type_multipart_mixed = "multipart/mixed" |
| const content_type_multipart_alternative = "multipart/alternative" |
| const content_type_multipart_related = "multipart/related" |
| const content_type_text_html = "text/html" |
| const content_type_text_plain = "text/plain" |
| |
| func Parse(r io.Reader) (email Email, err error) { |
| msg, err := mail.ReadMessage(r) |
| if err != nil { |
| return |
| } |
| |
| email, err = createEmailFromHeader(msg.Header) |
| if err != nil { |
| return |
| } |
| |
| contentType, params, err := parseContentType(msg.Header.Get("Content-Type")) |
| if err != nil { |
| return |
| } |
| |
| switch contentType { |
| case content_type_multipart_mixed: |
| email.TextBody, email.HTMLBody, email.Attachments, email.EmbeddedFiles, err = parseMultipartMixed(msg.Body, params["boundary"]) |
| case content_type_multipart_alternative: |
| email.TextBody, email.HTMLBody, email.EmbeddedFiles, err = parseMultipartAlternative(msg.Body, params["boundary"]) |
| case content_type_text_plain: |
| message, _ := ioutil.ReadAll(msg.Body) |
| email.TextBody = strings.TrimSuffix(string(message[:]), "\n") |
| case content_type_text_html: |
| message, _ := ioutil.ReadAll(msg.Body) |
| email.HTMLBody = strings.TrimSuffix(string(message[:]), "\n") |
| case "": |
| err = errors.New("No top level mime type specified") |
| default: |
| err = errors.New(fmt.Sprintf("Unknown top level mime type: %s", contentType)) |
| } |
| |
| return |
| } |
| |
| func createEmailFromHeader(header mail.Header) (email Email, err error) { |
| email.Subject = header.Get("Subject") |
| |
| email.From, err = parseAddressList(header.Get("From")) |
| if err != nil { |
| return |
| } |
| |
| email.Sender, err = parseAddress(header.Get("Sender")) |
| if err != nil { |
| return |
| } |
| |
| email.ReplyTo, err = parseAddressList(header.Get("Reply-To")) |
| if err != nil { |
| return |
| } |
| |
| email.To, err = parseAddressList(header.Get("To")) |
| if err != nil { |
| return |
| } |
| |
| email.Cc, err = parseAddressList(header.Get("Cc")) |
| if err != nil { |
| return |
| } |
| |
| email.Bcc, err = parseAddressList(header.Get("Bcc")) |
| if err != nil { |
| return |
| } |
| |
| email.Date, err = parseTime(header.Get("Date")) |
| if err != nil { |
| return |
| } |
| |
| email.ResentFrom, err = parseAddressList(header.Get("Resent-From")) |
| if err != nil { |
| return |
| } |
| |
| email.ResentSender, err = parseAddress(header.Get("Resent-Sender")) |
| if err != nil { |
| return |
| } |
| |
| email.ResentTo, err = parseAddressList(header.Get("Resent-To")) |
| if err != nil { |
| return |
| } |
| |
| email.ResentCc, err = parseAddressList(header.Get("Resent-Cc")) |
| if err != nil { |
| return |
| } |
| |
| email.ResentBcc, err = parseAddressList(header.Get("Resent-Bcc")) |
| if err != nil { |
| return |
| } |
| |
| if header.Get("Resent-Date") == "" { |
| email.ResentDate = time.Time{} |
| } else { |
| email.ResentDate, err = parseTime(header.Get("Resent-Date")) |
| if err != nil { |
| return |
| } |
| } |
| |
| email.ResentMessageID = parseMessageId(header.Get("Resent-Message-ID")) |
| email.MessageID = parseMessageId(header.Get("Message-ID")) |
| email.InReplyTo = parseMessageIdList(header.Get("In-Reply-To")) |
| email.References = parseMessageIdList(header.Get("References")) |
| |
| //decode whole header for easier access to extra fields |
| //todo: should we decode? aren't only standard fields mime encoded? |
| email.Header, err = decodeHeaderMime(header) |
| if err != nil { |
| return |
| } |
| |
| return |
| } |
| |
| func parseContentType(contentTypeHeader string) (contentType string, params map[string]string, err error) { |
| if contentTypeHeader == "" { |
| contentType = content_type_text_plain |
| return |
| } |
| |
| return mime.ParseMediaType(contentTypeHeader) |
| } |
| |
| func parseAddress(s string) (*mail.Address, error) { |
| if strings.Trim(s, " \n") != "" { |
| return mail.ParseAddress(s) |
| } |
| |
| return nil, nil |
| } |
| |
| func parseAddressList(s string) ([]*mail.Address, error) { |
| if strings.Trim(s, " \n") != "" { |
| return mail.ParseAddressList(s) |
| } |
| |
| return []*mail.Address{}, nil |
| } |
| |
| func parseTime(s string) (time.Time, error) { |
| t, err := time.Parse(time.RFC1123Z, s) |
| if err == nil { |
| return t, err |
| } |
| |
| return time.Parse("Mon, 2 Jan 2006 15:04:05 -0700", s) |
| } |
| |
| func parseMessageId(s string) string { |
| return strings.Trim(s, "<> ") |
| } |
| |
| func parseMessageIdList(s string) (result []string) { |
| for _, p := range strings.Split(s, " ") { |
| if strings.Trim(p, " \n") != "" { |
| result = append(result, parseMessageId(p)) |
| } |
| } |
| |
| return |
| } |
| |
| func parseMultipartAlternative(msg io.Reader, boundary string) (textBody, htmlBody string, embeddedFiles []EmbeddedFile, err error) { |
| pmr := multipart.NewReader(msg, boundary) |
| for { |
| part, err := pmr.NextPart() |
| |
| if err == io.EOF { |
| break |
| } else if err != nil { |
| return textBody, htmlBody, embeddedFiles, err |
| } |
| |
| contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type")) |
| |
| switch contentType { |
| case content_type_text_plain: |
| ppContent, err := ioutil.ReadAll(part) |
| if err != nil { |
| return textBody, htmlBody, embeddedFiles, err |
| } |
| |
| textBody += strings.TrimSuffix(string(ppContent[:]), "\n") |
| case content_type_text_html: |
| ppContent, err := ioutil.ReadAll(part) |
| if err != nil { |
| return textBody, htmlBody, embeddedFiles, err |
| } |
| |
| htmlBody += strings.TrimSuffix(string(ppContent[:]), "\n") |
| case content_type_multipart_related: |
| var tb, hb string |
| var ef []EmbeddedFile |
| tb, hb, ef, err = parseMultipartAlternative(part, params["boundary"]) |
| htmlBody += hb |
| textBody += tb |
| embeddedFiles = append(embeddedFiles, ef...) |
| default: |
| if isEmbeddedFile(part) { |
| ef, err := decodeEmbeddedFile(part) |
| if err != nil { |
| return textBody, htmlBody, embeddedFiles, err |
| } |
| |
| embeddedFiles = append(embeddedFiles, ef) |
| } else { |
| return textBody, htmlBody, embeddedFiles, errors.New(fmt.Sprintf("Can't process multipart/alternative inner mime type: %s", contentType)) |
| } |
| } |
| } |
| |
| return textBody, htmlBody, embeddedFiles, err |
| } |
| |
| func parseMultipartMixed(msg io.Reader, boundary string) (textBody, htmlBody string, attachments []Attachment, embeddedFiles []EmbeddedFile, err error) { |
| mr := multipart.NewReader(msg, boundary) |
| for { |
| part, err := mr.NextPart() |
| if err == io.EOF { |
| break |
| } else if err != nil { |
| return textBody, htmlBody, attachments, embeddedFiles, err |
| } |
| |
| contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type")) |
| if err != nil { |
| return textBody, htmlBody, attachments, embeddedFiles, err |
| } |
| |
| if contentType == content_type_multipart_alternative { |
| textBody, htmlBody, embeddedFiles, err = parseMultipartAlternative(part, params["boundary"]) |
| if err != nil { |
| return textBody, htmlBody, attachments, embeddedFiles, err |
| } |
| } else if isAttachment(part) { |
| at, err := decodeAttachment(part) |
| if err != nil { |
| return textBody, htmlBody, attachments, embeddedFiles, err |
| } |
| |
| attachments = append(attachments, at) |
| } else { |
| return textBody, htmlBody, attachments, embeddedFiles, errors.New(fmt.Sprintf("Unknown multipart/mixed nested mime type: %s", contentType)) |
| } |
| } |
| |
| return textBody, htmlBody, attachments, embeddedFiles, err |
| } |
| |
| func decodeMimeSentence(s string) (string, error) { |
| result := []string{} |
| ss := strings.Split(s, " ") |
| |
| for _, word := range ss { |
| dec := new(mime.WordDecoder) |
| w, err := dec.Decode(word) |
| if err != nil { |
| if len(result) == 0 { |
| w = word |
| } else { |
| w = " " + word |
| } |
| } |
| |
| result = append(result, w) |
| } |
| |
| return strings.Join(result, ""), nil |
| } |
| |
| func decodeHeaderMime(header mail.Header) (mail.Header, error) { |
| parsedHeader := map[string][]string{} |
| |
| for headerName, headerData := range header { |
| |
| parsedHeaderData := []string{} |
| for _, headerValue := range headerData { |
| decodedHeaderValue, err := decodeMimeSentence(headerValue) |
| if err != nil { |
| return mail.Header{}, err |
| } |
| parsedHeaderData = append(parsedHeaderData, decodedHeaderValue) |
| } |
| |
| parsedHeader[headerName] = parsedHeaderData |
| } |
| |
| return mail.Header(parsedHeader), nil |
| } |
| |
| func decodePartData(part *multipart.Part) (io.Reader, error) { |
| encoding := part.Header.Get("Content-Transfer-Encoding") |
| |
| if encoding == "base64" { |
| dr := base64.NewDecoder(base64.StdEncoding, part) |
| dd, err := ioutil.ReadAll(dr) |
| if err != nil { |
| return nil, err |
| } |
| |
| return bytes.NewReader(dd), nil |
| } else { |
| return nil, errors.New(fmt.Sprintf("Unknown encoding: %s", encoding)) |
| } |
| } |
| |
| func isEmbeddedFile(part *multipart.Part) bool { |
| return part.Header.Get("Content-Transfer-Encoding") != "" |
| } |
| |
| func decodeEmbeddedFile(part *multipart.Part) (ef EmbeddedFile, err error) { |
| cid, err := decodeMimeSentence(part.Header.Get("Content-Id")) |
| if err != nil { |
| return |
| } |
| |
| decoded, err := decodePartData(part) |
| if err != nil { |
| return |
| } |
| |
| ef.CID = strings.Trim(cid, "<>") |
| ef.Data = decoded |
| ef.ContentType = part.Header.Get("Content-Type") |
| |
| return |
| } |
| |
| func isAttachment(part *multipart.Part) bool { |
| return part.FileName() != "" |
| } |
| |
| func decodeAttachment(part *multipart.Part) (at Attachment, err error) { |
| filename, err := decodeMimeSentence(part.FileName()) |
| if err != nil { |
| return |
| } |
| |
| decoded, err := decodePartData(part) |
| if err != nil { |
| return |
| } |
| |
| at.Filename = filename |
| at.Data = decoded |
| at.ContentType = strings.Split(part.Header.Get("Content-Type"), ";")[0] |
| |
| return |
| } |
| |
| type Attachment struct { |
| Filename string |
| ContentType string |
| Data io.Reader |
| } |
| |
| type EmbeddedFile struct { |
| CID string |
| ContentType string |
| Data io.Reader |
| } |
| |
| type MessageID string |
| |
| type Email struct { |
| Header mail.Header |
| |
| Subject string |
| Sender *mail.Address |
| From []*mail.Address |
| ReplyTo []*mail.Address |
| To []*mail.Address |
| Cc []*mail.Address |
| Bcc []*mail.Address |
| Date time.Time |
| MessageID string |
| InReplyTo []string |
| References []string |
| |
| ResentFrom []*mail.Address |
| ResentSender *mail.Address |
| ResentTo []*mail.Address |
| ResentDate time.Time |
| ResentCc []*mail.Address |
| ResentBcc []*mail.Address |
| ResentMessageID string |
| |
| Received string |
| |
| HTMLBody string |
| TextBody string |
| |
| Attachments []Attachment |
| EmbeddedFiles []EmbeddedFile |
| } |