From 2e670d9e4572a76788e4a11696e5f95acde5e76f Mon Sep 17 00:00:00 2001 From: k-yomo Date: Thu, 30 Apr 2020 22:42:48 +0900 Subject: [PATCH] Add support for html in multipart/mixed --- parsemail.go | 7 +++++++ parsemail_test.go | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/parsemail.go b/parsemail.go index 42e0170..6a60192 100644 --- a/parsemail.go +++ b/parsemail.go @@ -249,6 +249,13 @@ func parseMultipartMixed(msg io.Reader, boundary string) (textBody, htmlBody str } textBody += strings.TrimSuffix(string(ppContent[:]), "\n") + } else if contentType == contentTypeTextHtml { + ppContent, err := ioutil.ReadAll(part) + if err != nil { + return textBody, htmlBody, attachments, embeddedFiles, err + } + + htmlBody += strings.TrimSuffix(string(ppContent[:]), "\n") } else if isAttachment(part) { at, err := decodeAttachment(part) if err != nil { diff --git a/parsemail_test.go b/parsemail_test.go index 3f3a825..109e734 100644 --- a/parsemail_test.go +++ b/parsemail_test.go @@ -296,6 +296,26 @@ So, "Hello".`, textBody: `plain text part`, }, 10: { + contentType: `multipart/mixed; boundary="0000000000007e2bb40587e36196"`, + mailData: textHTMLInMultipart, + subject: "Re: kern/54143 (virtualbox)", + from: []mail.Address{ + { + Name: "Rares", + Address: "rares@example.com", + }, + }, + to: []mail.Address{ + { + Name: "", + Address: "bugs@example.com", + }, + }, + date: parseDate("Fri, 02 May 2019 11:25:35 +0300"), + textBody: ``, + htmlBody: "
html text part



", + }, + 11: { mailData: rfc5322exampleA12WithTimezone, from: []mail.Address{ { @@ -331,7 +351,7 @@ So, "Hello".`, date: parseDate("Tue, 01 Jul 2003 10:52:37 +0200"), textBody: `Hi everyone.`, }, - 11: { + 12: { contentType: "multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf", mailData: attachment7bit, subject: "Peter Foobar", @@ -358,7 +378,7 @@ So, "Hello".`, }, }, }, - 12: { + 13: { contentType: "multipart/related; boundary=\"000000000000ab2e2205a26de587\"", mailData: multipartRelatedExample, subject: "Saying Hello", @@ -743,6 +763,20 @@ plain text part --0000000000007e2bb40587e36196-- ` +var textHTMLInMultipart = `From: Rares +Date: Thu, 2 May 2019 11:25:35 +0300 +Subject: Re: kern/54143 (virtualbox) +To: bugs@example.com +Content-Type: multipart/mixed; boundary="0000000000007e2bb40587e36196" + +--0000000000007e2bb40587e36196 +Content-Type: text/html; charset="UTF-8" + +
html text part



+ +--0000000000007e2bb40587e36196-- +` + var rfc5322exampleA11 = `From: John Doe Sender: Michael Jones To: Mary Smith