Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode text/plain and text/html body with encoding #2

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions parsemail.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,31 @@ func Parse(r io.Reader) (email Email, err error) {
email.TextBody, email.HTMLBody, email.EmbeddedFiles, err = parseMultipartRelated(msg.Body, params["boundary"])
case contentTypeTextPlain:
message, _ := ioutil.ReadAll(msg.Body)
var reader io.Reader
reader, err = decodeContent(strings.NewReader(string(message[:])), msg.Header.Get("Content-Transfer-Encoding"))
if err != nil {
return
}

message, err = ioutil.ReadAll(reader)
if err != nil {
return
}

email.TextBody = strings.TrimSuffix(string(message[:]), "\n")
case contentTypeTextHtml:
message, _ := ioutil.ReadAll(msg.Body)
var reader io.Reader
reader, err = decodeContent(strings.NewReader(string(message[:])), msg.Header.Get("Content-Transfer-Encoding"))
if err != nil {
return
}

message, err = ioutil.ReadAll(reader)
if err != nil {
return
}

email.HTMLBody = strings.TrimSuffix(string(message[:]), "\n")
default:
email.Content, err = decodeContent(msg.Body, msg.Header.Get("Content-Transfer-Encoding"))
Expand Down Expand Up @@ -121,14 +143,26 @@ func parseMultipartRelated(msg io.Reader, boundary string) (textBody, htmlBody s

switch contentType {
case contentTypeTextPlain:
ppContent, err := ioutil.ReadAll(part)
message, _ := ioutil.ReadAll(part)
reader, err := decodeContent(strings.NewReader(string(message[:])), part.Header.Get("Content-Transfer-Encoding"))
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}

ppContent, err := ioutil.ReadAll(reader)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}

textBody += strings.TrimSuffix(string(ppContent[:]), "\n")
case contentTypeTextHtml:
ppContent, err := ioutil.ReadAll(part)
message, _ := ioutil.ReadAll(part)
reader, err := decodeContent(strings.NewReader(string(message[:])), part.Header.Get("Content-Transfer-Encoding"))
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}

ppContent, err := ioutil.ReadAll(reader)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
Expand Down Expand Up @@ -178,14 +212,26 @@ func parseMultipartAlternative(msg io.Reader, boundary string) (textBody, htmlBo

switch contentType {
case contentTypeTextPlain:
ppContent, err := ioutil.ReadAll(part)
message, _ := ioutil.ReadAll(part)
reader, err := decodeContent(strings.NewReader(string(message[:])), part.Header.Get("Content-Transfer-Encoding"))
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}

ppContent, err := ioutil.ReadAll(reader)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}

textBody += strings.TrimSuffix(string(ppContent[:]), "\n")
case contentTypeTextHtml:
ppContent, err := ioutil.ReadAll(part)
message, _ := ioutil.ReadAll(part)
reader, err := decodeContent(strings.NewReader(string(message[:])), part.Header.Get("Content-Transfer-Encoding"))
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}

ppContent, err := ioutil.ReadAll(reader)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
Expand Down Expand Up @@ -361,6 +407,8 @@ func decodeContent(content io.Reader, encoding string) (io.Reader, error) {
}

return bytes.NewReader(dd), nil
case "8bit":
return content, nil
case "":
return content, nil
default:
Expand Down
49 changes: 49 additions & 0 deletions parsemail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,31 @@ So, "Hello".`,
htmlBody: "<div dir=\"ltr\"><div>Time for the egg.</div><div><br></div><div><br><br></div></div>",
textBody: "Time for the egg.",
},
14: {
contentType: "multipart/alternative; boundary=\"000000000000ab2e1f05a26de586\"",
mailData: base64Content,
subject: "Saying Hello",
from: []mail.Address{
{
Name: "John Doe",
Address: "[email protected]",
},
},
sender: mail.Address{
Name: "Michael Jones",
Address: "[email protected]",
},
to: []mail.Address{
{
Name: "Mary Smith",
Address: "[email protected]",
},
},
messageID: "[email protected]",
date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"),
htmlBody: "<div dir=\"ltr\">👍</div>",
textBody: "👍",
},
}

for index, td := range testData {
Expand Down Expand Up @@ -946,3 +971,27 @@ Content-Disposition: attachment;

--f403045f1dcc043a44054c8e6bbf--
`

var base64Content = `MIME-Version: 1.0
From: John Doe <[email protected]>
Sender: Michael Jones <[email protected]>
To: Mary Smith <[email protected]>
Subject: Saying Hello
Date: Fri, 21 Nov 1997 09:55:06 -0600
Message-ID: <[email protected]>
Content-Type: multipart/alternative; boundary="000000000000ab2e1f05a26de586"

--000000000000ab2e1f05a26de586
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: base64

8J+RjQo=

--000000000000ab2e1f05a26de586
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: base64

PGRpdiBkaXI9Imx0ciI+8J+RjTwvZGl2Pgo=

--000000000000ab2e1f05a26de586--
`