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 support for SMTP headers #7

Merged
merged 3 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion mail/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Message struct {
CC []string
Bcc []string
Subject string
Headers map[string]string

Bodies []Body
Attachments []Attachment
Expand Down Expand Up @@ -76,7 +77,12 @@ func (m *Message) AddAttachment(name, contentType string, r io.Reader) error {
return nil
}

// SetHeader sets the heder field and value for the message
func (m *Message) SetHeader(field, value string) {
m.Headers[field] = value
}

//NewMessage Builds a new message.
func NewMessage() Message {
return Message{}
return Message{Headers: map[string]string{}}
}
4 changes: 4 additions & 0 deletions mail/smtp_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (sm SMTPSender) Send(message Message) error {
m.Attach(at.Name, settings)
}

for field, value := range message.Headers {
m.SetHeader(field, value)
}

err := sm.Dialer.DialAndSend(m)

if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions mail/smtp_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ func TestSendPlain(t *testing.T) {
smtpServer.Clear()
r := require.New(t)

m := mail.Message{
From: "[email protected]",
To: []string{"[email protected]"},
Subject: "Cool Message",
CC: []string{"[email protected]", "[email protected]"},
Bcc: []string{"[email protected]"},
}
m := mail.NewMessage()
m.From = "[email protected]"
m.To = []string{"[email protected]"}
m.Subject = "Cool Message"
m.CC = []string{"[email protected]", "[email protected]"}
m.Bcc = []string{"[email protected]"}

m.AddAttachment("someFile.txt", "text/plain", bytes.NewBuffer([]byte("hello")))
m.AddBody(rend.String("Hello <%= Name %>"), render.Data{"Name": "Antonio"})
r.Equal(m.Bodies[0].Content, "Hello Antonio")

m.SetHeader("X-SMTPAPI", `{"send_at": 1409348513}`)

err := sender.Send(m)
r.Nil(err)

Expand All @@ -55,4 +56,5 @@ func TestSendPlain(t *testing.T) {
r.Contains(lastMessage, "Hello Antonio")
r.Contains(lastMessage, "Content-Disposition: attachment; filename=\"someFile.txt\"")
r.Contains(lastMessage, "aGVsbG8=") //base64 of the file content
r.Contains(lastMessage, `X-SMTPAPI: {"send_at": 1409348513}`)
}