From 1c68d4769e8dc98a9dd91794164e54871803624c Mon Sep 17 00:00:00 2001 From: Anatolij Ostroumov Date: Sun, 11 Feb 2018 21:07:20 +0300 Subject: [PATCH 1/2] add SetBoundary(boundary string) method to manually set mime boundary sting --- message.go | 13 +++++++++++++ message_test.go | 32 ++++++++++++++++++++++++++++++++ writeto.go | 13 +++++++++---- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/message.go b/message.go index 4bffb1e..38bb7dd 100644 --- a/message.go +++ b/message.go @@ -18,6 +18,7 @@ type Message struct { encoding Encoding hEncoder mimeEncoder buf bytes.Buffer + boundary string } type header map[string][]string @@ -69,6 +70,13 @@ func (m *Message) applySettings(settings []MessageSetting) { // email. type MessageSetting func(m *Message) +// SetBoundary is a message setting to set mime separator string +func SetBoundary(boundary string) MessageSetting { + return func(m *Message) { + m.boundary = boundary + } +} + // SetCharset is a message setting to set the charset of the email. func SetCharset(charset string) MessageSetting { return func(m *Message) { @@ -97,6 +105,11 @@ const ( Unencoded Encoding = "8bit" ) +// SetBoundary allows to set custom boundary for multypart message generated +func (m *Message) SetBoundary(boundary string) { + m.boundary = boundary +} + // SetHeader sets a value to the given header field. func (m *Message) SetHeader(field string, value ...string) { m.encodeHeader(value) diff --git a/message_test.go b/message_test.go index acceff2..1c5f4e1 100644 --- a/message_test.go +++ b/message_test.go @@ -200,6 +200,38 @@ func TestPartSetting(t *testing.T) { testMessage(t, m, 1, want) } +func TestPartSettingWithCustomBoundary(t *testing.T) { + m := NewMessage() + m.SetBoundary("lalalaDaiMne3Ryblya") + m.SetHeader("From", "from@example.com") + m.SetHeader("To", "to@example.com") + m.SetBody("text/plain; format=flowed", "¡Hola, señor!", SetPartEncoding(Unencoded)) + m.AddAlternative("text/html", "¡Hola, señor!") + + 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;\r\n" + + " boundary=lalalaDaiMne3Ryblya\r\n" + + "\r\n" + + "--lalalaDaiMne3Ryblya\r\n" + + "Content-Type: text/plain; format=flowed; charset=UTF-8\r\n" + + "Content-Transfer-Encoding: 8bit\r\n" + + "\r\n" + + "¡Hola, señor!\r\n" + + "--lalalaDaiMne3Ryblya\r\n" + + "Content-Type: text/html; charset=UTF-8\r\n" + + "Content-Transfer-Encoding: quoted-printable\r\n" + + "\r\n" + + "=C2=A1Hola, se=C3=B1or!\r\n" + + "--lalalaDaiMne3Ryblya--\r\n", + } + + testMessage(t, m, 1, want) +} + func TestBodyWriter(t *testing.T) { m := NewMessage() m.SetHeader("From", "from@example.com") diff --git a/writeto.go b/writeto.go index 9fb6b86..0e3face 100644 --- a/writeto.go +++ b/writeto.go @@ -28,15 +28,15 @@ func (w *messageWriter) writeMessage(m *Message) { w.writeHeaders(m.header) if m.hasMixedPart() { - w.openMultipart("mixed") + w.openMultipart("mixed", m.boundary) } if m.hasRelatedPart() { - w.openMultipart("related") + w.openMultipart("related", m.boundary) } if m.hasAlternativePart() { - w.openMultipart("alternative") + w.openMultipart("alternative", m.boundary) } for _, part := range m.parts { w.writePart(part, m.charset) @@ -77,8 +77,13 @@ type messageWriter struct { err error } -func (w *messageWriter) openMultipart(mimeType string) { +func (w *messageWriter) openMultipart(mimeType, boundary string) { mw := multipart.NewWriter(w) + if boundary == "" { + boundary = mw.Boundary() + } else { + mw.SetBoundary(boundary) + } contentType := "multipart/" + mimeType + ";\r\n boundary=" + mw.Boundary() w.writers[w.depth] = mw From 92322ba9dc49057b5fbbcb0e8d79efe7a2747fd4 Mon Sep 17 00:00:00 2001 From: Anatolij Ostroumov Date: Thu, 15 Feb 2018 16:06:23 +0300 Subject: [PATCH 2/2] simplify code --- writeto.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/writeto.go b/writeto.go index 0e3face..91b06c5 100644 --- a/writeto.go +++ b/writeto.go @@ -79,9 +79,7 @@ type messageWriter struct { func (w *messageWriter) openMultipart(mimeType, boundary string) { mw := multipart.NewWriter(w) - if boundary == "" { - boundary = mw.Boundary() - } else { + if boundary != "" { mw.SetBoundary(boundary) } contentType := "multipart/" + mimeType + ";\r\n boundary=" + mw.Boundary()