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

Fix to check attachment before swtich case #4

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 8 additions & 7 deletions parsemail.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ func parseMultipartMixed(msg io.Reader, boundary string, depth int) (textBody, h
} else if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, textBodies, htmlBodies, err
}
if isAttachment(part) {
at, err := decodeAttachment(part)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, textBodies, htmlBodies, err
}
attachments = append(attachments, at)
continue
}
contentType, params := part.contentType, part.contentTypeParams
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, textBodies, htmlBodies, err
Expand Down Expand Up @@ -403,13 +411,6 @@ func parseMultipartMixed(msg io.Reader, boundary string, depth int) (textBody, h
at.Filename = decodeMimeSentence(name)
}
}
attachments = append(attachments, at)
} else if isAttachment(part) {
at, err := decodeAttachment(part)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, textBodies, htmlBodies, err
}

attachments = append(attachments, at)
} else {
return textBody, htmlBody, attachments, embeddedFiles, textBodies, htmlBodies, fmt.Errorf("Unknown multipart/mixed nested mime type: %s", contentType)
Expand Down
62 changes: 62 additions & 0 deletions parsemail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,37 @@ So, "Hello".`,
},
},
},
15: {
contentType: "multipart/signed; protocol=\"application/pkcs7-signature\"; micalg=sha-256; boundary=\"----=_Part_746216_364383494.1698130589208\"",
mailData: htmlAttachmentExample,
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: "PGRpdiBkaXI9Imx0ciI+PGRpdj5UaGlzIGlzIGEgcmVjZWlwdC48L2Rpdj48ZGl2Pjxicj48L2Rp\ndj48ZGl2Pjxicj48YnI+PC9kaXY+PC9kaXY+",
attachments: []attachmentData{
{
filename: "Receipt.html",
contentType: "text/html",
data: "<div dir=\"ltr\"><div>This is a receipt.</div><div><br></div><div><br><br></div></div>",
},
},
},
}

for index, td := range testData {
Expand Down Expand Up @@ -1021,3 +1052,34 @@ Content-Description: S/MIME Cryptographic Signature

------=_Part_746216_364383494.1698130589208--
`

var htmlAttachmentExample = `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]>
Subject: ooops
To: [email protected]
Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha-256;
boundary="----=_Part_746216_364383494.1698130589208"
X-Comments:
Feedback-ID: 1.ap-northeast-1.Pw8FL2ZI4Ah4ultEiAJulZI5IJxZ/+eKa4j/3x7HjXU=:AmazonSES
X-SES-Outgoing: 2023.10.24-23.251.234.52

------=_Part_746216_364383494.1698130589208
Content-Transfer-Encoding: base64
Content-Type: text/html; charset=UTF-8

PGRpdiBkaXI9Imx0ciI+PGRpdj5UaGlzIGlzIGEgcmVjZWlwdC48L2Rpdj48ZGl2Pjxicj48L2Rp
dj48ZGl2Pjxicj48YnI+PC9kaXY+PC9kaXY+
------=_Part_746216_364383494.1698130589208
Content-Disposition: attachment; filename="Receipt.html"
Content-Transfer-Encoding: base64
Content-Type: text/html; charset=utf-8; name="Receipt.html"

PGRpdiBkaXI9Imx0ciI+PGRpdj5UaGlzIGlzIGEgcmVjZWlwdC48L2Rpdj48ZGl2Pjxicj48L2Rp
dj48ZGl2Pjxicj48YnI+PC9kaXY+PC9kaXY+
------=_Part_746216_364383494.1698130589208--
`