From e66d2198f408e5c862dde731c890b9353f56bb36 Mon Sep 17 00:00:00 2001 From: Tarmo Randma Date: Tue, 28 Oct 2025 14:09:58 +0200 Subject: [PATCH 1/2] Improved repeated body MIME handling (eg.Apple-Mail repeated HTML block) --- email.go | 2 +- email_test.go | 33 ++++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/email.go b/email.go index 239d2e9..f4cab57 100644 --- a/email.go +++ b/email.go @@ -236,7 +236,7 @@ func NewEmailFromReader(r io.Reader) (*Email, error) { switch { case ct == "text/plain" && len(e.Text) == 0: e.Text = p.body - case ct == "text/html": + case ct == "text/html" && len(e.HTML) == 0: e.HTML = p.body } } diff --git a/email_test.go b/email_test.go index 5dd0434..6ca0a62 100644 --- a/email_test.go +++ b/email_test.go @@ -528,7 +528,8 @@ func TestMultipleSameMimeFromReader(t *testing.T) { Subject: "Test Subject", To: []string{"Jordan Wright "}, From: "Jordan Wright ", - Text: []byte("This is a test email with HTML Formatting. It also has very long lines so\nthat the content must be wrapped if using quoted-printable decoding.\n"), + Text: []byte("A.1: This is UTF-8 text/plain body\n"), + HTML: []byte("B.1: This is first text/html body\n"), } raw := []byte(` MIME-Version: 1.0 @@ -540,16 +541,35 @@ Content-Type: multipart/alternative; boundary=001a114fb3fc42fd6b051f834280 --001a114fb3fc42fd6b051f834280 Content-Type: text/plain; charset=UTF-8 -This is a test email with HTML Formatting. It also has very long lines so -that the content must be wrapped if using quoted-printable decoding. +A.1: This is UTF-8 text/plain body --001a114fb3fc42fd6b051f834280 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii -This is second text-plain content-type imitating Apple-Mail legacy compatibility -functionality that adds the same body also in us-ascii after utf-8 version +A.2: This is fallback text/plain body + +--001a114fb3fc42fd6b051f834280 +Content-Type: multipart/mixed; + boundary="Apple-Mail=_2D67308F-5DF4-4EE4-BD05-2A7BC99B92F1" + + +--Apple-Mail=_2D67308F-5DF4-4EE4-BD05-2A7BC99B92F1 +Content-Transfer-Encoding: quoted-printable +Content-Type: text/html; + charset=utf-8 + +B.1: This is first text/html body + +--Apple-Mail=_2D67308F-5DF4-4EE4-BD05-2A7BC99B92F1 +Content-Transfer-Encoding: quoted-printable +Content-Type: text/html; + charset=utf-8 + +B.2: This is second text/html body + +--Apple-Mail=_2D67308F-5DF4-4EE4-BD05-2A7BC99B92F1-- --001a114fb3fc42fd6b051f834280--`) e, err := NewEmailFromReader(bytes.NewReader(raw)) @@ -562,6 +582,9 @@ functionality that adds the same body also in us-ascii after utf-8 version if !bytes.Equal(e.Text, ex.Text) { t.Fatalf("Incorrect text: %#q != %#q", e.Text, ex.Text) } + if !bytes.Equal(e.HTML, ex.HTML) { + t.Fatalf("Incorrect HTML: %#q != %#q", e.HTML, ex.HTML) + } if e.From != ex.From { t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From) } From 89641e4aca4296f267d3a9ea22b1a4b8a0f79199 Mon Sep 17 00:00:00 2001 From: Tarmo Randma Date: Tue, 28 Oct 2025 14:42:11 +0200 Subject: [PATCH 2/2] Example functions renamed to satisfy github --- email_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/email_test.go b/email_test.go index 6ca0a62..aa9772c 100644 --- a/email_test.go +++ b/email_test.go @@ -850,7 +850,7 @@ TGV0J3MganVzdCBwcmV0ZW5kIHRoaXMgaXMgcmF3IEpQRUcgZGF0YS4= } } -func ExampleGmail() { +func ExampleNewEmail_gmail() { e := NewEmail() e.From = "Jordan Wright " e.To = []string{"test@example.com"} @@ -859,12 +859,12 @@ func ExampleGmail() { e.Subject = "Awesome Subject" e.Text = []byte("Text Body is, of course, supported!\n") e.HTML = []byte("

Fancy Html is supported, too!

\n") - e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com")) + _ = e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com")) } -func ExampleAttach() { +func ExampleEmail_AttachFile() { e := NewEmail() - e.AttachFile("test.txt") + _, _ = e.AttachFile("test.txt") } func Test_base64Wrap(t *testing.T) {