Skip to content

Commit 943e75f

Browse files
authored
Adds support for text/html with base64 CTE (#138)
base64 was not supported when emails had a single text/* part encoded in base64 Added relevant code & test. Fixes #137
1 parent 3656afa commit 943e75f

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

email.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,11 @@ func parseMIMEParts(hs textproto.MIMEHeader, b io.Reader) ([]*part, error) {
243243
}
244244
} else {
245245
// If it is not a multipart email, parse the body content as a single "part"
246-
if hs.Get("Content-Transfer-Encoding") == "quoted-printable" {
246+
switch hs.Get("Content-Transfer-Encoding") {
247+
case "quoted-printable":
247248
b = quotedprintable.NewReader(b)
248-
249+
case "base64":
250+
b = base64.NewDecoder(base64.StdEncoding, b)
249251
}
250252
var buf bytes.Buffer
251253
if _, err := io.Copy(&buf, b); err != nil {

email_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,28 @@ Testing!
837837
}
838838
}
839839

840+
func TestNoMultipartHTMLContentTypeBase64Encoding(t *testing.T) {
841+
raw := []byte(`MIME-Version: 1.0
842+
843+
844+
Date: 7 Jan 2021 03:07:44 -0800
845+
Subject: Hello
846+
Content-Type: text/html; charset=utf-8
847+
Content-Transfer-Encoding: base64
848+
Message-Id: <[email protected]>
849+
850+
PGh0bWw+PGhlYWQ+PHRpdGxlPnRlc3Q8L3RpdGxlPjwvaGVhZD48Ym9keT5IZWxsbyB3
851+
b3JsZCE8L2JvZHk+PC9odG1sPg==
852+
`)
853+
e, err := NewEmailFromReader(bytes.NewReader(raw))
854+
if err != nil {
855+
t.Fatalf("Error when parsing email %s", err.Error())
856+
}
857+
if !bytes.Equal(e.HTML, []byte("<html><head><title>test</title></head><body>Hello world!</body></html>")) {
858+
t.Fatalf("Error incorrect text: %#q != %#q\n", e.Text, "<html>...</html>")
859+
}
860+
}
861+
840862
// *Since the mime library in use by ```email``` is now in the stdlib, this test is deprecated
841863
func Test_quotedPrintDecode(t *testing.T) {
842864
text := []byte("Dear reader!\r\n\r\n" +

0 commit comments

Comments
 (0)