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

Find Attachments, dont matter the content-type msg #25

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
77 changes: 64 additions & 13 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 @@ -232,6 +278,14 @@ func parseMultipartMixed(msg io.Reader, boundary string) (textBody, htmlBody str
return textBody, htmlBody, attachments, embeddedFiles, err
}

if isAttachment(part) {
at, err := decodeAttachment(part)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}
attachments = append(attachments, at)
}

if contentType == contentTypeMultipartAlternative {
textBody, htmlBody, embeddedFiles, err = parseMultipartAlternative(part, params["boundary"])
if err != nil {
Expand All @@ -256,13 +310,6 @@ func parseMultipartMixed(msg io.Reader, boundary string) (textBody, htmlBody str
}

htmlBody += strings.TrimSuffix(string(ppContent[:]), "\n")
} else if isAttachment(part) {
at, err := decodeAttachment(part)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}

attachments = append(attachments, at)
} else {
return textBody, htmlBody, attachments, embeddedFiles, fmt.Errorf("Unknown multipart/mixed nested mime type: %s", contentType)
}
Expand Down Expand Up @@ -345,6 +392,8 @@ func decodeAttachment(part *multipart.Part) (at Attachment, err error) {
}

func decodeContent(content io.Reader, encoding string) (io.Reader, error) {
encoding = strings.ToLower(encoding)

switch encoding {
case "base64":
decoded := base64.NewDecoder(base64.StdEncoding, content)
Expand All @@ -361,6 +410,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 Expand Up @@ -483,11 +534,11 @@ type Email struct {
ResentMessageID string

ContentType string
Content io.Reader
Content io.Reader

HTMLBody string
TextBody string

Attachments []Attachment
EmbeddedFiles []EmbeddedFile
}
}
71 changes: 60 additions & 11 deletions parsemail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ So, "Hello".`,
messageID: "[email protected]",
date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"),
contentType: `image/jpeg; x-unix-mode=0644; name="image.gif"`,
content: `GIF89a;`,
content: `GIF89a;`,
},
9: {
contentType: `multipart/mixed; boundary="0000000000007e2bb40587e36196"`,
Expand Down Expand Up @@ -372,15 +372,15 @@ So, "Hello".`,
htmlBody: "<div dir=\"ltr\"><br></div>",
attachments: []attachmentData{
{
filename: "unencoded.csv",
contentType: "application/csv",
data: fmt.Sprintf("\n"+`"%s", "%s", "%s", "%s", "%s"`+"\n"+`"%s", "%s", "%s", "%s", "%s"`+"\n", "Some", "Data", "In", "Csv", "Format", "Foo", "Bar", "Baz", "Bum", "Poo"),
filename: "unencoded.csv",
contentType: "application/csv",
data: fmt.Sprintf("\n"+`"%s", "%s", "%s", "%s", "%s"`+"\n"+`"%s", "%s", "%s", "%s", "%s"`+"\n", "Some", "Data", "In", "Csv", "Format", "Foo", "Bar", "Baz", "Bum", "Poo"),
},
},
},
13: {
contentType: "multipart/related; boundary=\"000000000000ab2e2205a26de587\"",
mailData: multipartRelatedExample,
mailData: multipartRelatedExample,
subject: "Saying Hello",
from: []mail.Address{
{
Expand All @@ -389,7 +389,7 @@ So, "Hello".`,
},
},
sender: mail.Address{
Name: "Michael Jones",
Name: "Michael Jones",
Address: "[email protected]",
},
to: []mail.Address{
Expand All @@ -401,7 +401,32 @@ So, "Hello".`,
messageID: "[email protected]",
date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"),
htmlBody: "<div dir=\"ltr\"><div>Time for the egg.</div><div><br></div><div><br><br></div></div>",
textBody: "Time for the egg.",
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: "👍",
},
}

Expand Down Expand Up @@ -595,9 +620,9 @@ func parseDate(in string) time.Time {
}

type attachmentData struct {
filename string
contentType string
data string
filename string
contentType string
data string
}

type embeddedFileData struct {
Expand Down Expand Up @@ -879,7 +904,7 @@ Message-ID: <[email protected]>
Content-Type: image/jpeg;
x-unix-mode=0644;
name="image.gif"
Content-Transfer-Encoding: base64
Content-Transfer-Encoding: Base64

R0lGODlhAQE7`

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--
`