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/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
+}