From fc3e6459ae886c53e20cfb15cb0e0f4e9bdf9f9f Mon Sep 17 00:00:00 2001 From: Edwin Love Date: Tue, 2 Apr 2019 11:17:35 +0100 Subject: [PATCH 1/4] Implement changes from PR in order to get tests passing https://github.com/DusanKasan/parsemail/pull/5/files --- parsemail_test.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/parsemail_test.go b/parsemail_test.go index e0457bb..809aad8 100644 --- a/parsemail_test.go +++ b/parsemail_test.go @@ -248,11 +248,7 @@ So, "Hello".`, } for index, td := range testData { - m, err := mail.ReadMessage(strings.NewReader(td.mailData)) - if err != nil { - t.Error(err) - } - e, err := Parse(m) + e, err := Parse(strings.NewReader(td.mailData)) if err != nil { t.Error(err) } From 2ed156ab867647ec43bb859fe4f5dfb519bc9cb8 Mon Sep 17 00:00:00 2001 From: Edwin Love Date: Tue, 2 Apr 2019 11:18:37 +0100 Subject: [PATCH 2/4] Add go.mod file --- go.mod | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..010a846 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/EdwinLove/parsemail + +go 1.12 From f8d8da2e32010f61216ab56bf0f9fe1fb8392d95 Mon Sep 17 00:00:00 2001 From: Edwin Love Date: Tue, 2 Apr 2019 14:07:32 +0100 Subject: [PATCH 3/4] Write failing test for email with unencoded attachment --- parsemail_test.go | 83 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 6 deletions(-) diff --git a/parsemail_test.go b/parsemail_test.go index 809aad8..3e66364 100644 --- a/parsemail_test.go +++ b/parsemail_test.go @@ -3,6 +3,7 @@ package parsemail import ( "encoding/base64" "io/ioutil" + "fmt" "net/mail" "strings" "testing" @@ -245,6 +246,33 @@ So, "Hello".`, }, }, }, + 8: { + mailData: data3, + subject: "Peter Foobar", + from: []mail.Address{ + { + Name: "Peter Foobar", + Address: "peter.foobar@gmail.com", + }, + }, + to: []mail.Address{ + { + Name: "", + Address: "dusan@kasan.sk", + }, + }, + messageID: "CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com", + date: parseDate("Tue, 02 Apr 2019 11:12:26 +0000"), + htmlBody: "

", + attachments: []attachmentData{ + { + filename: "unencoded.csv", + contentType: "application/csv", + unencodedData: 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 { @@ -363,9 +391,16 @@ So, "Hello".`, if err != nil { t.Error(err) } - - encoded := base64.StdEncoding.EncodeToString(b) - if ra.Filename == ad.filename && encoded == ad.base64data && ra.ContentType == ad.contentType { + actual := "actual" + expected := "expected" + if ad.base64data != "" { + actual = base64.StdEncoding.EncodeToString(b) + expected = ad.base64data + } else if ad.unencodedData != "" { + actual = string(b) + expected = ad.unencodedData + } + if ra.Filename == ad.filename && actual == expected && ra.ContentType == ad.contentType { found = true attachs = append(attachs[:i], attachs[i+1:]...) } @@ -425,9 +460,10 @@ func parseDate(in string) time.Time { } type attachmentData struct { - filename string - contentType string - base64data string + filename string + contentType string + base64data string + unencodedData string } type embeddedFileData struct { @@ -583,6 +619,41 @@ YKUKF+Os3baUndC0pDnwNAmLy1SUr2Gw0luxQuV/AwC6cEhVV5VRrwAAAABJRU5ErkJggg== --------------C70C0458A558E585ACB75FB4-- ` +var data3 = `From: =?UTF-8?Q?Peter_Foobar?= +Date: Tue, 2 Apr 2019 11:12:26 +0000 +Message-ID: +Subject: =?UTF-8?Q?Peter_Foobar?= +To: dusan@kasan.sk +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 + +

+ +--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-- +` + var rfc5322exampleA11 = `From: John Doe Sender: Michael Jones To: Mary Smith From 71e25d5dadf3793a515cf39540084fcf25028c8a Mon Sep 17 00:00:00 2001 From: Edwin Love Date: Tue, 2 Apr 2019 14:07:44 +0100 Subject: [PATCH 4/4] Let parser handle email with unencoded attachment --- parsemail.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/parsemail.go b/parsemail.go index 996e357..a62426a 100644 --- a/parsemail.go +++ b/parsemail.go @@ -301,6 +301,13 @@ func decodePartData(part *multipart.Part) (io.Reader, error) { return nil, err } + return bytes.NewReader(dd), nil + } else if strings.EqualFold(encoding, "7bit") { + dd, err := ioutil.ReadAll(part) + if err != nil { + return nil, err + } + return bytes.NewReader(dd), nil }