From e3f729829b62ee7b9e90c6fcba99631eedb7e425 Mon Sep 17 00:00:00 2001 From: isch Date: Thu, 14 Jan 2021 17:53:10 +0500 Subject: [PATCH 1/3] Added parsing attachments in non-multipart emails --- parsemail.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/parsemail.go b/parsemail.go index 6a60192..dce4382 100644 --- a/parsemail.go +++ b/parsemail.go @@ -18,6 +18,7 @@ const contentTypeMultipartAlternative = "multipart/alternative" const contentTypeMultipartRelated = "multipart/related" const contentTypeTextHtml = "text/html" const contentTypeTextPlain = "text/plain" +const contentTypeOctetStream = "application/octet-stream" // Parse an email message read from io.Reader into parsemail.Email struct func Parse(r io.Reader) (email Email, err error) { @@ -50,6 +51,8 @@ func Parse(r io.Reader) (email Email, err error) { case contentTypeTextHtml: message, _ := ioutil.ReadAll(msg.Body) email.HTMLBody = strings.TrimSuffix(string(message[:]), "\n") + case contentTypeOctetStream: + email.Attachments, err = parseAttachmentOnlyEmail(msg.Body, msg.Header) default: email.Content, err = decodeContent(msg.Body, msg.Header.Get("Content-Transfer-Encoding")) } @@ -103,6 +106,29 @@ func parseContentType(contentTypeHeader string) (contentType string, params map[ return mime.ParseMediaType(contentTypeHeader) } +func parseAttachmentOnlyEmail(body io.Reader, header mail.Header) (attachments []Attachment, err error) { + attachmentData, err := decodeContent(body, header.Get("Content-Transfer-Encoding")) + contentDisposition := header.Get("Content-Disposition") + + if err != nil { + return attachments, err + } + + if len(contentDisposition) > 0 && strings.Contains(contentDisposition, "attachment;") { + fileName := strings.Replace(contentDisposition, "attachment; filename=\"", "", -1) + fileName = strings.TrimRight(fileName, "\"") + + at := Attachment{ + Filename: fileName, + ContentType: "application/octet-stream", + Data: attachmentData, + } + attachments = append(attachments, at) + } + + return attachments, nil +} + func parseMultipartRelated(msg io.Reader, boundary string) (textBody, htmlBody string, embeddedFiles []EmbeddedFile, err error) { pmr := multipart.NewReader(msg, boundary) for { @@ -483,11 +509,11 @@ type Email struct { ResentMessageID string ContentType string - Content io.Reader + Content io.Reader HTMLBody string TextBody string Attachments []Attachment EmbeddedFiles []EmbeddedFile -} \ No newline at end of file +} From 9e7b7f4ceaaf045b785bdc339385fe5068e10fea Mon Sep 17 00:00:00 2001 From: isch Date: Fri, 15 Jan 2021 09:18:06 +0500 Subject: [PATCH 2/3] Small fix --- parsemail.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/parsemail.go b/parsemail.go index dce4382..533ed69 100644 --- a/parsemail.go +++ b/parsemail.go @@ -107,14 +107,15 @@ func parseContentType(contentTypeHeader string) (contentType string, params map[ } func parseAttachmentOnlyEmail(body io.Reader, header mail.Header) (attachments []Attachment, err error) { - attachmentData, err := decodeContent(body, header.Get("Content-Transfer-Encoding")) contentDisposition := header.Get("Content-Disposition") - if err != nil { - return attachments, err - } - if len(contentDisposition) > 0 && strings.Contains(contentDisposition, "attachment;") { + + attachmentData, err := decodeContent(body, header.Get("Content-Transfer-Encoding")) + if err != nil { + return attachments, err + } + fileName := strings.Replace(contentDisposition, "attachment; filename=\"", "", -1) fileName = strings.TrimRight(fileName, "\"") @@ -123,6 +124,7 @@ func parseAttachmentOnlyEmail(body io.Reader, header mail.Header) (attachments [ ContentType: "application/octet-stream", Data: attachmentData, } + attachments = append(attachments, at) } From e9e98d0c72c653f70b2b972033b91a2a842c13f4 Mon Sep 17 00:00:00 2001 From: isch Date: Fri, 15 Jan 2021 12:12:30 +0500 Subject: [PATCH 3/3] Changed module --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 5b91a53..5c9602d 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/DusanKasan/parsemail +module github.com/get-net/parsemail go 1.12 \ No newline at end of file