Fixed a bug in AddAlternativeWriter
AddAlternativeWriter was replacing the message body instead of adding a body part. Fixes #44
This commit is contained in:
parent
33430073ec
commit
df6fc79d10
12
message.go
12
message.go
|
@ -197,26 +197,22 @@ func (m *Message) SetBody(contentType, body string) {
|
||||||
//
|
//
|
||||||
// More info: http://en.wikipedia.org/wiki/MIME#Alternative
|
// More info: http://en.wikipedia.org/wiki/MIME#Alternative
|
||||||
func (m *Message) AddAlternative(contentType, body string) {
|
func (m *Message) AddAlternative(contentType, body string) {
|
||||||
m.parts = append(m.parts,
|
m.parts = append(m.parts, part{
|
||||||
part{
|
|
||||||
header: m.getPartHeader(contentType),
|
header: m.getPartHeader(contentType),
|
||||||
copier: func(w io.Writer) error {
|
copier: func(w io.Writer) error {
|
||||||
_, err := io.WriteString(w, body)
|
_, err := io.WriteString(w, body)
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddAlternativeWriter adds an alternative part to the message. It can be
|
// AddAlternativeWriter adds an alternative part to the message. It can be
|
||||||
// useful with the text/template or html/template packages.
|
// useful with the text/template or html/template packages.
|
||||||
func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error) {
|
func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error) {
|
||||||
m.parts = []part{
|
m.parts = append(m.parts, part{
|
||||||
{
|
|
||||||
header: m.getPartHeader(contentType),
|
header: m.getPartHeader(contentType),
|
||||||
copier: f,
|
copier: f,
|
||||||
},
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Message) getPartHeader(contentType string) header {
|
func (m *Message) getPartHeader(contentType string) header {
|
||||||
|
|
|
@ -63,29 +63,6 @@ func TestMessage(t *testing.T) {
|
||||||
testMessage(t, m, 0, want)
|
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) {
|
func TestCustomMessage(t *testing.T) {
|
||||||
m := NewMessage(SetCharset("ISO-8859-1"), SetEncoding(Base64))
|
m := NewMessage(SetCharset("ISO-8859-1"), SetEncoding(Base64))
|
||||||
m.SetHeaders(map[string][]string{
|
m.SetHeaders(map[string][]string{
|
||||||
|
@ -191,6 +168,42 @@ func TestAlternative(t *testing.T) {
|
||||||
testMessage(t, m, 1, want)
|
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) {
|
func TestAttachmentOnly(t *testing.T) {
|
||||||
m := NewMessage()
|
m := NewMessage()
|
||||||
m.SetHeader("From", "from@example.com")
|
m.SetHeader("From", "from@example.com")
|
||||||
|
|
Loading…
Reference in New Issue