Skip to content

Commit

Permalink
Add support for HTML alternative message body
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wneessen committed Feb 5, 2024
1 parent 6808857 commit 2f60d9c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions msgwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2f60d9c

Please sign in to comment.