Fixed a bug in AddAlternativeWriter

AddAlternativeWriter was replacing the message body instead
of adding a body part.

Fixes #44
This commit is contained in:
Alexandre Cesaro 2015-11-05 20:31:01 +01:00
parent 33430073ec
commit df6fc79d10
2 changed files with 46 additions and 37 deletions

View File

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

View File

@ -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")