From 2f60d9c5df9a71844f06e19c429bf7ad46110fe9 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Mon, 5 Feb 2024 11:46:06 +0100 Subject: [PATCH] Add support for HTML alternative message body The code has been updated to allow for an HTML alternative to the plain text body in messages. This includes adding a new function to add this alternative body along with additional unit tests to ensure the multipart messages are properly constructed. The tests also check the correct usage of different charsets. --- msgwriter_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/msgwriter_test.go b/msgwriter_test.go index d19d334..4f61c6f 100644 --- a/msgwriter_test.go +++ b/msgwriter_test.go @@ -53,6 +53,7 @@ func TestMsgWriter_writeMsg(t *testing.T) { m.SetDateWithValue(now) m.SetMessageIDWithValue("message@id.com") m.SetBodyString(TypeTextPlain, "This is the body") + m.AddAlternativeString(TypeTextHTML, "This is the alternative body") buf := bytes.Buffer{} mw := &msgWriter{w: &buf, c: CharsetUTF8, en: mime.QEncoding} mw.writeMsg(m) @@ -95,6 +96,26 @@ func TestMsgWriter_writeMsg(t *testing.T) { if !strings.Contains(ms, "\r\n\r\nThis is the body") { ea = append(ea, "Message body") } + + pl := m.GetParts() + if len(pl) <= 0 { + t.Errorf("expected multiple parts but got none") + return + } + if len(pl) == 2 { + ap := pl[1] + ap.SetCharset(CharsetISO88591) + } + buf.Reset() + mw.writeMsg(m) + ms = buf.String() + if !strings.Contains(ms, "\r\n\r\nThis is the alternative body") { + ea = append(ea, "Message alternative body") + } + if !strings.Contains(ms, `Content-Type: text/html; charset=ISO-8859-1`) { + ea = append(ea, "alternative body charset") + } + if len(ea) > 0 { em := "writeMsg() failed. The following errors occurred:\n" for e := range ea {