-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'EdwinLove-seven-bit-attachment'
- Loading branch information
Showing
3 changed files
with
96 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/EdwinLove/parsemail | ||
|
||
go 1.12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package parsemail | |
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"io/ioutil" | ||
"net/mail" | ||
"strings" | ||
|
@@ -183,10 +184,10 @@ So, "Hello".`, | |
So, "Hello".`, | ||
}, | ||
6: { | ||
mailData: data1, | ||
mailData: data1, | ||
contentType: `multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf`, | ||
content: "", | ||
subject: "Peter Paholík", | ||
content: "", | ||
subject: "Peter Paholík", | ||
from: []mail.Address{ | ||
{ | ||
Name: "Peter Paholík", | ||
|
@@ -204,17 +205,17 @@ So, "Hello".`, | |
htmlBody: "<div dir=\"ltr\"><br></div>", | ||
attachments: []attachmentData{ | ||
{ | ||
filename: "Peter Paholík 1 4 2017 2017-04-07.pdf", | ||
contentType: "application/pdf", | ||
base64data: "JVBERi0xLjQNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDY3IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4vT3V0cHV0SW50ZW50c1s8PC9UeXBlL091dHB1dEludGVudC9TL0dUU19QREZBMS9PdXRwdXRDb25kZXYgMzk1MzYyDQo+Pg0Kc3RhcnR4cmVmDQo0MTk4ODUNCiUlRU9GDQo=", | ||
filename: "Peter Paholík 1 4 2017 2017-04-07.json", | ||
contentType: "application/json", | ||
data: "[1, 2, 3]", | ||
}, | ||
}, | ||
}, | ||
7: { | ||
mailData: data2, | ||
mailData: data2, | ||
contentType: `multipart/alternative; boundary="------------C70C0458A558E585ACB75FB4"`, | ||
content: "", | ||
subject: "Re: Test Subject 2", | ||
content: "", | ||
subject: "Re: Test Subject 2", | ||
from: []mail.Address{ | ||
{ | ||
Name: "Sender Man", | ||
|
@@ -270,15 +271,15 @@ So, "Hello".`, | |
Name: "Michael Jones", | ||
Address: "[email protected]", | ||
}, | ||
messageID: "[email protected]", | ||
date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"), | ||
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"`, | ||
mailData: textPlainInMultipart, | ||
subject: "Re: kern/54143 (virtualbox)", | ||
mailData: textPlainInMultipart, | ||
subject: "Re: kern/54143 (virtualbox)", | ||
from: []mail.Address{ | ||
{ | ||
Name: "Rares", | ||
|
@@ -291,7 +292,7 @@ So, "Hello".`, | |
Address: "[email protected]", | ||
}, | ||
}, | ||
date: parseDate("Fri, 02 May 2019 11:25:35 +0300"), | ||
date: parseDate("Fri, 02 May 2019 11:25:35 +0300"), | ||
textBody: `plain text part`, | ||
}, | ||
10: { | ||
|
@@ -330,6 +331,33 @@ So, "Hello".`, | |
date: parseDate("Tue, 01 Jul 2003 10:52:37 +0200"), | ||
textBody: `Hi everyone.`, | ||
}, | ||
11: { | ||
contentType: "multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf", | ||
mailData: attachment7bit, | ||
subject: "Peter Foobar", | ||
from: []mail.Address{ | ||
{ | ||
Name: "Peter Foobar", | ||
Address: "[email protected]", | ||
}, | ||
}, | ||
to: []mail.Address{ | ||
{ | ||
Name: "", | ||
Address: "[email protected]", | ||
}, | ||
}, | ||
messageID: "CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com", | ||
date: parseDate("Tue, 02 Apr 2019 11:12:26 +0000"), | ||
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"), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for index, td := range testData { | ||
|
@@ -462,8 +490,7 @@ So, "Hello".`, | |
t.Error(err) | ||
} | ||
|
||
encoded := base64.StdEncoding.EncodeToString(b) | ||
if ra.Filename == ad.filename && encoded == ad.base64data && ra.ContentType == ad.contentType { | ||
if ra.Filename == ad.filename && string(b) == ad.data && ra.ContentType == ad.contentType { | ||
found = true | ||
attachs = append(attachs[:i], attachs[i+1:]...) | ||
} | ||
|
@@ -523,9 +550,9 @@ func parseDate(in string) time.Time { | |
} | ||
|
||
type attachmentData struct { | ||
filename string | ||
contentType string | ||
base64data string | ||
filename string | ||
contentType string | ||
data string | ||
} | ||
|
||
type embeddedFileData struct { | ||
|
@@ -616,19 +643,16 @@ Content-Type: text/html; charset=UTF-8 | |
--f403045f1dcc043a3f054c8e6bbd-- | ||
--f403045f1dcc043a44054c8e6bbf | ||
Content-Type: application/pdf; | ||
Content-Type: application/json; | ||
name="=?UTF-8?Q?Peter_Paholi=CC=81k_1?= | ||
=?UTF-8?Q?_4_2017_2017=2D04=2D07=2Epdf?=" | ||
=?UTF-8?Q?_4_2017_2017=2D04=2D07=2Ejson?=" | ||
Content-Disposition: attachment; | ||
filename="=?UTF-8?Q?Peter_Paholi=CC=81k_1?= | ||
=?UTF-8?Q?_4_2017_2017=2D04=2D07=2Epdf?=" | ||
=?UTF-8?Q?_4_2017_2017=2D04=2D07=2Ejson?=" | ||
Content-Transfer-Encoding: base64 | ||
X-Attachment-Id: f_j17i0f0d0 | ||
JVBERi0xLjQNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu | ||
Zyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDY3IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4v | ||
T3V0cHV0SW50ZW50c1s8PC9UeXBlL091dHB1dEludGVudC9TL0dUU19QREZBMS9PdXRwdXRDb25k | ||
ZXYgMzk1MzYyDQo+Pg0Kc3RhcnR4cmVmDQo0MTk4ODUNCiUlRU9GDQo= | ||
WzEsIDIsIDNd | ||
--f403045f1dcc043a44054c8e6bbf-- | ||
` | ||
|
||
|
@@ -723,7 +747,6 @@ Message-ID: <[email protected]> | |
Hi everyone. | ||
` | ||
|
||
|
||
//todo: not yet implemented in net/mail | ||
//once there is support for this, add it | ||
var rfc5322exampleA13 = `From: Pete <[email protected]> | ||
|
@@ -800,3 +823,38 @@ Content-Type: image/jpeg; | |
Content-Transfer-Encoding: base64 | ||
R0lGODlhAQE7` | ||
|
||
var attachment7bit = `From: =?UTF-8?Q?Peter_Foobar?= <[email protected]> | ||
Date: Tue, 2 Apr 2019 11:12:26 +0000 | ||
Message-ID: <CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com> | ||
Subject: =?UTF-8?Q?Peter_Foobar?= | ||
To: [email protected] | ||
Content-Type: multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf | ||
--f403045f1dcc043a44054c8e6bbf | ||
Content-Type: multipart/alternative; boundary=f403045f1dcc043a3f054c8e6bbd | ||
--f403045f1dcc043a3f054c8e6bbd | ||
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/csv; | ||
name="unencoded.csv" | ||
Content-Transfer-Encoding: 7bit | ||
Content-Disposition: attachment; | ||
filename="unencoded.csv" | ||
"Some", "Data", "In", "Csv", "Format" | ||
"Foo", "Bar", "Baz", "Bum", "Poo" | ||
--f403045f1dcc043a44054c8e6bbf-- | ||
` |