Skip to content

Commit

Permalink
Merge pull request #4 from ThalysonR/feat/ignore_empty_content_type
Browse files Browse the repository at this point in the history
Feat: Ignore Empty Content-Type on Multipart
  • Loading branch information
tatorodrigo authored Jan 12, 2022
2 parents f2ddf2e + 4c97e98 commit 13dc5ce
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
21 changes: 18 additions & 3 deletions parsemail.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ func parseMultipartRelated(msg io.Reader, boundary string) (textBody, htmlBody s
return textBody, htmlBody, embeddedFiles, err
}

contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type"))
hContentType := strings.TrimSpace(part.Header.Get("Content-Type"))
if len(hContentType) == 0 {
continue
}

contentType, params, err := mime.ParseMediaType(hContentType)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
Expand Down Expand Up @@ -171,7 +176,12 @@ func parseMultipartAlternative(msg io.Reader, boundary string) (textBody, htmlBo
return textBody, htmlBody, embeddedFiles, err
}

contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type"))
hContentType := strings.TrimSpace(part.Header.Get("Content-Type"))
if len(hContentType) == 0 {
continue
}

contentType, params, err := mime.ParseMediaType(hContentType)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
Expand Down Expand Up @@ -227,7 +237,12 @@ func parseMultipartMixed(msg io.Reader, boundary string) (textBody, htmlBody str
return textBody, htmlBody, attachments, embeddedFiles, err
}

contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type"))
hContentType := strings.TrimSpace(part.Header.Get("Content-Type"))
if len(hContentType) == 0 {
continue
}

contentType, params, err := mime.ParseMediaType(hContentType)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}
Expand Down
61 changes: 61 additions & 0 deletions parsemail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,34 @@ So, "Hello".`,
},
},
},
16: {
mailData: data4,
contentType: `multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf`,
content: "",
subject: "Peter Paholík",
from: []mail.Address{
{
Name: "Peter Paholík",
Address: "[email protected]",
},
},
to: []mail.Address{
{
Name: "",
Address: "[email protected]",
},
},
messageID: "CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com",
date: parseDate("Fri, 07 Apr 2017 09:17:26 +0200"),
htmlBody: "<div dir=\"ltr\"><br></div>",
attachments: []attachmentData{
{
filename: "Peter Paholík 1 4 2017 2017-04-07.json",
contentType: "application/json",
data: "[1, 2, 3]",
},
},
},
}

for index, td := range testData {
Expand Down Expand Up @@ -820,6 +848,39 @@ Content-Type: text/plain; charset=UTF-8
--f403045f1dcc043a3f054c8e6bbd
Content-Type: text/html; charset=UTF-8
<div dir="ltr"><br></div>
--f403045f1dcc043a3f054c8e6bbd--
--f403045f1dcc043a44054c8e6bbf
Content-Type: application/json;
name="=?UTF-8?Q?Peter_Paholi=CC=81k_1?=
=?UTF-8?Q?_4_2017_2017=2D04=2D07=2Ejson?="
Content-Disposition: attachment
Content-Transfer-Encoding: base64
X-Attachment-Id: f_j17i0f0d0
WzEsIDIsIDNd
--f403045f1dcc043a44054c8e6bbf--
`

var data4 = `From: =?UTF-8?Q?Peter_Pahol=C3=ADk?= <[email protected]>
Date: Fri, 7 Apr 2017 09:17:26 +0200
Message-ID: <CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com>
Subject: =?UTF-8?Q?Peter_Pahol=C3=ADk?=
To: [email protected]
Content-Type: multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf
--f403045f1dcc043a44054c8e6bbf
Content-Type: multipart/alternative; boundary=f403045f1dcc043a3f054c8e6bbd
--f403045f1dcc043a3f054c8e6bbd
Content-Transfer-Encoding: 7bit
--f403045f1dcc043a3f054c8e6bbd
Content-Type: text/html; charset=UTF-8
Expand Down

0 comments on commit 13dc5ce

Please sign in to comment.