Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SetBoundary(boundary string) method #109

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Message struct {
encoding Encoding
hEncoder mimeEncoder
buf bytes.Buffer
boundary string
}

type header map[string][]string
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]")
m.SetHeader("To", "[email protected]")
m.SetBody("text/plain; format=flowed", "¡Hola, señor!", SetPartEncoding(Unencoded))
m.AddAlternative("text/html", "¡<b>Hola</b>, <i>señor</i>!</h1>")

want := &message{
from: "[email protected]",
to: []string{"[email protected]"},
content: "From: [email protected]\r\n" +
"To: [email protected]\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=A1<b>Hola</b>, <i>se=C3=B1or</i>!</h1>\r\n" +
"--lalalaDaiMne3Ryblya--\r\n",
}

testMessage(t, m, 1, want)
}

func TestBodyWriter(t *testing.T) {
m := NewMessage()
m.SetHeader("From", "[email protected]")
Expand Down
13 changes: 9 additions & 4 deletions writeto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Copy link

@ivy ivy Feb 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, it doesn't look like there's any need for this assignment unless we decide to use it on L87 in place of mw.Boundary().

Instead, this could be expressed as:

if boundary != "" {
	mw.SetBoundary(boundary)
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, thanks

} else {
mw.SetBoundary(boundary)
}
contentType := "multipart/" + mimeType + ";\r\n boundary=" + mw.Boundary()
w.writers[w.depth] = mw

Expand Down