Skip to content

Commit c001ac5

Browse files
author
Winni Neessen
authored
Merge pull request #10 from wneessen/fix-8
Fixes #8
2 parents 570b7bc + ebef1fe commit c001ac5

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

msg.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,8 @@ func (m *Msg) GetRecipients() ([]string, error) {
390390
func (m *Msg) SetBodyString(ct ContentType, b string, o ...PartOption) {
391391
buf := bytes.NewBufferString(b)
392392
w := func(w io.Writer) (int64, error) {
393-
nb, err := io.Copy(w, buf)
394-
return nb, err
393+
nb, err := w.Write(buf.Bytes())
394+
return int64(nb), err
395395
}
396396
m.SetBodyWriter(ct, w, o...)
397397
}
@@ -407,8 +407,8 @@ func (m *Msg) SetBodyWriter(ct ContentType, w func(io.Writer) (int64, error), o
407407
func (m *Msg) AddAlternativeString(ct ContentType, b string, o ...PartOption) {
408408
buf := bytes.NewBufferString(b)
409409
w := func(w io.Writer) (int64, error) {
410-
nb, err := io.Copy(w, buf)
411-
return nb, err
410+
nb, err := w.Write(buf.Bytes())
411+
return int64(nb), err
412412
}
413413
m.AddAlternativeWriter(ct, w, o...)
414414
}

msg_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"net/mail"
9+
"strings"
910
"testing"
1011
"time"
1112
)
@@ -1244,3 +1245,30 @@ func TestMsg_appendFile(t *testing.T) {
12441245
t.Errorf("appendFile() failed. Expected length: %d, got: %d", 2, len(fl))
12451246
}
12461247
}
1248+
1249+
// TestMsg_multipleWrites tests multiple executions of WriteTo on the Msg
1250+
func TestMsg_multipleWrites(t *testing.T) {
1251+
ts := "XXX_UNIQUE_STRING_XXX"
1252+
wbuf := bytes.Buffer{}
1253+
m := NewMsg()
1254+
m.SetBodyString(TypeTextPlain, ts)
1255+
1256+
// First WriteTo()
1257+
_, err := m.WriteTo(&wbuf)
1258+
if err != nil {
1259+
t.Errorf("failed to write body to buffer: %s", err)
1260+
}
1261+
if !strings.Contains(wbuf.String(), ts) {
1262+
t.Errorf("first WriteTo() body does not contain unique string: %s", ts)
1263+
}
1264+
1265+
// Second WriteTo()
1266+
wbuf.Reset()
1267+
_, err = m.WriteTo(&wbuf)
1268+
if err != nil {
1269+
t.Errorf("failed to write body to buffer: %s", err)
1270+
}
1271+
if !strings.Contains(wbuf.String(), ts) {
1272+
t.Errorf("second WriteTo() body does not contain unique string: %s", ts)
1273+
}
1274+
}

0 commit comments

Comments
 (0)