Fix support for base64 content
- Sometimes the content-type is set as Base64 instead of base64. This
change adds support for the Base64 value.
- Parts inside a multipart message which have the
Content-Transfer-Encoding header set are now decoded (before the raw
text was returned).
diff --git a/README.md b/README.md
index 382b921..4eec42e 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,8 @@
+**Note:** This is a fork of the original library at
+https://github.com/DusanKasan/parsemail
+
+---
+
# Parsemail - simple email parsing Go library
[![Build Status](https://circleci.com/gh/DusanKasan/parsemail.svg?style=shield&circle-token=:circle-token)](https://circleci.com/gh/DusanKasan/parsemail) [![Coverage Status](https://coveralls.io/repos/github/DusanKasan/Parsemail/badge.svg?branch=master)](https://coveralls.io/github/DusanKasan/Parsemail?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/DusanKasan/parsemail)](https://goreportcard.com/report/github.com/DusanKasan/parsemail)
@@ -55,4 +60,4 @@
fmt.Println(a.ContentType)
//and read a.Data
}
-```
\ No newline at end of file
+```
diff --git a/parsemail.go b/parsemail.go
index 6a60192..1d21f58 100644
--- a/parsemail.go
+++ b/parsemail.go
@@ -121,14 +121,24 @@
switch contentType {
case contentTypeTextPlain:
- ppContent, err := ioutil.ReadAll(part)
+ buf, err := decodeContent(part, part.Header.Get("Content-Transfer-Encoding"))
+ if err != nil {
+ return textBody, htmlBody, embeddedFiles, err
+ }
+
+ ppContent, err := ioutil.ReadAll(buf)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
textBody += strings.TrimSuffix(string(ppContent[:]), "\n")
case contentTypeTextHtml:
- ppContent, err := ioutil.ReadAll(part)
+ buf, err := decodeContent(part, part.Header.Get("Content-Transfer-Encoding"))
+ if err != nil {
+ return textBody, htmlBody, embeddedFiles, err
+ }
+
+ ppContent, err := ioutil.ReadAll(buf)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
@@ -178,14 +188,24 @@
switch contentType {
case contentTypeTextPlain:
- ppContent, err := ioutil.ReadAll(part)
+ buf, err := decodeContent(part, part.Header.Get("Content-Transfer-Encoding"))
+ if err != nil {
+ return textBody, htmlBody, embeddedFiles, err
+ }
+
+ ppContent, err := ioutil.ReadAll(buf)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
textBody += strings.TrimSuffix(string(ppContent[:]), "\n")
case contentTypeTextHtml:
- ppContent, err := ioutil.ReadAll(part)
+ buf, err := decodeContent(part, part.Header.Get("Content-Transfer-Encoding"))
+ if err != nil {
+ return textBody, htmlBody, embeddedFiles, err
+ }
+
+ ppContent, err := ioutil.ReadAll(buf)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
@@ -243,14 +263,24 @@
return textBody, htmlBody, attachments, embeddedFiles, err
}
} else if contentType == contentTypeTextPlain {
- ppContent, err := ioutil.ReadAll(part)
+ buf, err := decodeContent(part, part.Header.Get("Content-Transfer-Encoding"))
+ if err != nil {
+ return textBody, htmlBody, attachments, embeddedFiles, err
+ }
+
+ ppContent, err := ioutil.ReadAll(buf)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}
textBody += strings.TrimSuffix(string(ppContent[:]), "\n")
} else if contentType == contentTypeTextHtml {
- ppContent, err := ioutil.ReadAll(part)
+ buf, err := decodeContent(part, part.Header.Get("Content-Transfer-Encoding"))
+ if err != nil {
+ return textBody, htmlBody, attachments, embeddedFiles, err
+ }
+
+ ppContent, err := ioutil.ReadAll(buf)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}
@@ -346,7 +376,7 @@
func decodeContent(content io.Reader, encoding string) (io.Reader, error) {
switch encoding {
- case "base64":
+ case "base64", "Base64":
decoded := base64.NewDecoder(base64.StdEncoding, content)
b, err := ioutil.ReadAll(decoded)
if err != nil {
@@ -490,4 +520,4 @@
Attachments []Attachment
EmbeddedFiles []EmbeddedFile
-}
\ No newline at end of file
+}