Merge pull request #2 from kevinmichaelchen/master
Case insensitive checks. Fix check for multipart/related.
diff --git a/README.md b/README.md
index 4dc1eaf..382b921 100644
--- a/README.md
+++ b/README.md
@@ -41,7 +41,7 @@
## Retrieving embedded files
-You can access embedded files in the same way you can access attachments. THey contain the mime type, data stream and content id that is used to reference them through the email.
+You can access embedded files in the same way you can access attachments. They contain the mime type, data stream and content id that is used to reference them through the email.
```go
var reader io.Reader
diff --git a/parsemail.go b/parsemail.go
index 6ee0624..996e357 100644
--- a/parsemail.go
+++ b/parsemail.go
@@ -100,6 +100,63 @@
return mime.ParseMediaType(contentTypeHeader)
}
+func parseMultipartRelated(msg io.Reader, boundary string) (textBody, htmlBody string, embeddedFiles []EmbeddedFile, err error) {
+ pmr := multipart.NewReader(msg, boundary)
+ for {
+ part, err := pmr.NextPart()
+
+ if err == io.EOF {
+ break
+ } else if err != nil {
+ return textBody, htmlBody, embeddedFiles, err
+ }
+
+ contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type"))
+ if err != nil {
+ return textBody, htmlBody, embeddedFiles, err
+ }
+
+ switch contentType {
+ case contentTypeTextPlain:
+ ppContent, err := ioutil.ReadAll(part)
+ if err != nil {
+ return textBody, htmlBody, embeddedFiles, err
+ }
+
+ textBody += strings.TrimSuffix(string(ppContent[:]), "\n")
+ case contentTypeTextHtml:
+ ppContent, err := ioutil.ReadAll(part)
+ if err != nil {
+ return textBody, htmlBody, embeddedFiles, err
+ }
+
+ htmlBody += strings.TrimSuffix(string(ppContent[:]), "\n")
+ case contentTypeMultipartAlternative:
+ tb, hb, ef, err := parseMultipartAlternative(part, params["boundary"])
+ if err != nil {
+ return textBody, htmlBody, embeddedFiles, err
+ }
+
+ htmlBody += hb
+ textBody += tb
+ embeddedFiles = append(embeddedFiles, ef...)
+ default:
+ if isEmbeddedFile(part) {
+ ef, err := decodeEmbeddedFile(part)
+ if err != nil {
+ return textBody, htmlBody, embeddedFiles, err
+ }
+
+ embeddedFiles = append(embeddedFiles, ef)
+ } else {
+ return textBody, htmlBody, embeddedFiles, fmt.Errorf("Can't process multipart/related inner mime type: %s", contentType)
+ }
+ }
+ }
+
+ return textBody, htmlBody, embeddedFiles, err
+}
+
func parseMultipartAlternative(msg io.Reader, boundary string) (textBody, htmlBody string, embeddedFiles []EmbeddedFile, err error) {
pmr := multipart.NewReader(msg, boundary)
for {
@@ -132,7 +189,7 @@
htmlBody += strings.TrimSuffix(string(ppContent[:]), "\n")
case contentTypeMultipartRelated:
- tb, hb, ef, err := parseMultipartAlternative(part, params["boundary"])
+ tb, hb, ef, err := parseMultipartRelated(part, params["boundary"])
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
@@ -177,6 +234,11 @@
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}
+ } else if contentType == contentTypeMultipartRelated {
+ textBody, htmlBody, embeddedFiles, err = parseMultipartRelated(part, params["boundary"])
+ if err != nil {
+ return textBody, htmlBody, attachments, embeddedFiles, err
+ }
} else if isAttachment(part) {
at, err := decodeAttachment(part)
if err != nil {
@@ -232,7 +294,7 @@
func decodePartData(part *multipart.Part) (io.Reader, error) {
encoding := part.Header.Get("Content-Transfer-Encoding")
- if encoding == "base64" {
+ if strings.EqualFold(encoding, "base64") {
dr := base64.NewDecoder(base64.StdEncoding, part)
dd, err := ioutil.ReadAll(dr)
if err != nil {