From df6fc79d10c2f49e005ca44dd3df7e053bf19c27 Mon Sep 17 00:00:00 2001 From: Alexandre Cesaro Date: Thu, 5 Nov 2015 20:31:01 +0100 Subject: [PATCH] Fixed a bug in AddAlternativeWriter AddAlternativeWriter was replacing the message body instead of adding a body part. Fixes #44 --- message.go | 24 +++++++++----------- message_test.go | 59 ++++++++++++++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/message.go b/message.go index 2f75368..2b6c0e8 100644 --- a/message.go +++ b/message.go @@ -197,26 +197,22 @@ func (m *Message) SetBody(contentType, body string) { // // More info: http://en.wikipedia.org/wiki/MIME#Alternative func (m *Message) AddAlternative(contentType, body string) { - m.parts = append(m.parts, - part{ - header: m.getPartHeader(contentType), - copier: func(w io.Writer) error { - _, err := io.WriteString(w, body) - return err - }, + m.parts = append(m.parts, part{ + header: m.getPartHeader(contentType), + copier: func(w io.Writer) error { + _, err := io.WriteString(w, body) + return err }, - ) + }) } // AddAlternativeWriter adds an alternative part to the message. It can be // useful with the text/template or html/template packages. func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error) { - m.parts = []part{ - { - header: m.getPartHeader(contentType), - copier: f, - }, - } + m.parts = append(m.parts, part{ + header: m.getPartHeader(contentType), + copier: f, + }) } func (m *Message) getPartHeader(contentType string) header { diff --git a/message_test.go b/message_test.go index 1876a7e..04c987e 100644 --- a/message_test.go +++ b/message_test.go @@ -63,29 +63,6 @@ func TestMessage(t *testing.T) { testMessage(t, m, 0, want) } -func TestBodyWriter(t *testing.T) { - m := NewMessage() - m.SetHeader("From", "from@example.com") - m.SetHeader("To", "to@example.com") - m.AddAlternativeWriter("text/plain", func(w io.Writer) error { - _, err := w.Write([]byte("Test message")) - return err - }) - - want := &message{ - from: "from@example.com", - to: []string{"to@example.com"}, - content: "From: from@example.com\r\n" + - "To: to@example.com\r\n" + - "Content-Type: text/plain; charset=UTF-8\r\n" + - "Content-Transfer-Encoding: quoted-printable\r\n" + - "\r\n" + - "Test message", - } - - testMessage(t, m, 0, want) -} - func TestCustomMessage(t *testing.T) { m := NewMessage(SetCharset("ISO-8859-1"), SetEncoding(Base64)) m.SetHeaders(map[string][]string{ @@ -191,6 +168,42 @@ func TestAlternative(t *testing.T) { testMessage(t, m, 1, want) } +func TestBodyWriter(t *testing.T) { + m := NewMessage() + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") + m.AddAlternativeWriter("text/plain", func(w io.Writer) error { + _, err := w.Write([]byte("Test message")) + return err + }) + m.AddAlternativeWriter("text/html", func(w io.Writer) error { + _, err := w.Write([]byte("Test HTML")) + return err + }) + + want := &message{ + from: "from@example.com", + to: []string{"to@example.com"}, + content: "From: from@example.com\r\n" + + "To: to@example.com\r\n" + + "Content-Type: multipart/alternative; boundary=_BOUNDARY_1_\r\n" + + "\r\n" + + "--_BOUNDARY_1_\r\n" + + "Content-Type: text/plain; charset=UTF-8\r\n" + + "Content-Transfer-Encoding: quoted-printable\r\n" + + "\r\n" + + "Test message\r\n" + + "--_BOUNDARY_1_\r\n" + + "Content-Type: text/html; charset=UTF-8\r\n" + + "Content-Transfer-Encoding: quoted-printable\r\n" + + "\r\n" + + "Test HTML\r\n" + + "--_BOUNDARY_1_--\r\n", + } + + testMessage(t, m, 1, want) +} + func TestAttachmentOnly(t *testing.T) { m := NewMessage() m.SetHeader("From", "from@example.com")