add support for arbitrary top level content type
diff --git a/parsemail.go b/parsemail.go
index 996e357..d37e82c 100644
--- a/parsemail.go
+++ b/parsemail.go
@@ -31,11 +31,13 @@
return
}
- contentType, params, err := parseContentType(msg.Header.Get("Content-Type"))
+ email.ContentType = msg.Header.Get("Content-Type")
+ contentType, params, err := parseContentType(email.ContentType)
if err != nil {
return
}
+
switch contentType {
case contentTypeMultipartMixed:
email.TextBody, email.HTMLBody, email.Attachments, email.EmbeddedFiles, err = parseMultipartMixed(msg.Body, params["boundary"])
@@ -48,7 +50,7 @@
message, _ := ioutil.ReadAll(msg.Body)
email.HTMLBody = strings.TrimSuffix(string(message[:]), "\n")
default:
- err = fmt.Errorf("Unknown top level mime type: %s", contentType)
+ email.Content, err = decodeContent(msg.Body, msg.Header.Get("Content-Transfer-Encoding"))
}
return
@@ -291,29 +293,13 @@
return mail.Header(parsedHeader), nil
}
-func decodePartData(part *multipart.Part) (io.Reader, error) {
- encoding := part.Header.Get("Content-Transfer-Encoding")
-
- if strings.EqualFold(encoding, "base64") {
- dr := base64.NewDecoder(base64.StdEncoding, part)
- dd, err := ioutil.ReadAll(dr)
- if err != nil {
- return nil, err
- }
-
- return bytes.NewReader(dd), nil
- }
-
- return nil, fmt.Errorf("Unknown encoding: %s", encoding)
-}
-
func isEmbeddedFile(part *multipart.Part) bool {
return part.Header.Get("Content-Transfer-Encoding") != ""
}
func decodeEmbeddedFile(part *multipart.Part) (ef EmbeddedFile, err error) {
cid := decodeMimeSentence(part.Header.Get("Content-Id"))
- decoded, err := decodePartData(part)
+ decoded, err := decodeContent(part, part.Header.Get("Content-Transfer-Encoding"))
if err != nil {
return
}
@@ -331,7 +317,7 @@
func decodeAttachment(part *multipart.Part) (at Attachment, err error) {
filename := decodeMimeSentence(part.FileName())
- decoded, err := decodePartData(part)
+ decoded, err := decodeContent(part, part.Header.Get("Content-Transfer-Encoding"))
if err != nil {
return
}
@@ -343,6 +329,23 @@
return
}
+func decodeContent(content io.Reader, encoding string) (io.Reader, error) {
+ switch encoding {
+ case "base64":
+ decoded := base64.NewDecoder(base64.StdEncoding, content)
+ b, err := ioutil.ReadAll(decoded)
+ if err != nil {
+ return nil, err
+ }
+
+ return bytes.NewReader(b), nil
+ case "":
+ return content, nil
+ default:
+ return nil, fmt.Errorf("unknown encoding: %s", encoding)
+ }
+}
+
type headerParser struct {
header *mail.Header
err error
@@ -450,9 +453,12 @@
ResentBcc []*mail.Address
ResentMessageID string
+ ContentType string
+ Content io.Reader
+
HTMLBody string
TextBody string
Attachments []Attachment
EmbeddedFiles []EmbeddedFile
-}
+}
\ No newline at end of file
diff --git a/parsemail_test.go b/parsemail_test.go
index e0457bb..6796a29 100644
--- a/parsemail_test.go
+++ b/parsemail_test.go
@@ -13,6 +13,8 @@
var testData = map[int]struct {
mailData string
+ contentType string
+ content string
subject string
date time.Time
from []mail.Address
@@ -182,6 +184,8 @@
},
6: {
mailData: data1,
+ contentType: `multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf`,
+ content: "",
subject: "Peter Paholík",
from: []mail.Address{
{
@@ -208,6 +212,8 @@
},
7: {
mailData: data2,
+ contentType: `multipart/alternative; boundary="------------C70C0458A558E585ACB75FB4"`,
+ content: "",
subject: "Re: Test Subject 2",
from: []mail.Address{
{
@@ -245,16 +251,49 @@
},
},
},
+ 8: {
+ mailData: imageContentExample,
+ subject: "Saying Hello",
+ from: []mail.Address{
+ {
+ Name: "John Doe",
+ Address: "jdoe@machine.example",
+ },
+ },
+ to: []mail.Address{
+ {
+ Name: "Mary Smith",
+ Address: "mary@example.net",
+ },
+ },
+ sender: mail.Address{
+ Name: "Michael Jones",
+ Address: "mjones@machine.example",
+ },
+ messageID: "1234@local.machine.example",
+ date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"),
+ contentType: `image/jpeg; x-unix-mode=0644; name="image.gif"`,
+ content: `GIF89a;`,
+ },
}
for index, td := range testData {
- m, err := mail.ReadMessage(strings.NewReader(td.mailData))
+ e, err := Parse(strings.NewReader(td.mailData))
if err != nil {
t.Error(err)
}
- e, err := Parse(m)
- if err != nil {
- t.Error(err)
+
+ if td.contentType != e.ContentType {
+ t.Errorf("[Test Case %v] Wrong content type. Expected: %s, Got: %s", index, td.contentType, e.ContentType)
+ }
+
+ if td.content != "" {
+ b, err := ioutil.ReadAll(e.Content)
+ if err != nil {
+ t.Error(err)
+ } else if td.content != string(b) {
+ t.Errorf("[Test Case %v] Wrong content. Expected: %s, Got: %s", index, td.content, string(b))
+ }
}
if td.subject != e.Subject {
@@ -670,3 +709,16 @@
This is a message just to say hello.
So, "Hello".`
+
+var imageContentExample = `From: John Doe <jdoe@machine.example>
+Sender: Michael Jones <mjones@machine.example>
+To: Mary Smith <mary@example.net>
+Subject: Saying Hello
+Date: Fri, 21 Nov 1997 09:55:06 -0600
+Message-ID: <1234@local.machine.example>
+Content-Type: image/jpeg;
+ x-unix-mode=0644;
+ name="image.gif"
+Content-Transfer-Encoding: base64
+
+R0lGODlhAQE7`