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.
This commit is contained in:
Winni Neessen 2024-02-05 11:46:06 +01:00
parent 68088577ba
commit 2f60d9c5df
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D

View file

@ -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 {