diff --git a/mail/message.go b/mail/message.go index 7c9295e..fd807cc 100644 --- a/mail/message.go +++ b/mail/message.go @@ -15,6 +15,7 @@ type Message struct { CC []string Bcc []string Subject string + Headers map[string]string Bodies []Body Attachments []Attachment @@ -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{}} } diff --git a/mail/smtp_sender.go b/mail/smtp_sender.go index 987a411..50f8a4f 100644 --- a/mail/smtp_sender.go +++ b/mail/smtp_sender.go @@ -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 { diff --git a/mail/smtp_sender_test.go b/mail/smtp_sender_test.go index 9521db9..46bf05b 100644 --- a/mail/smtp_sender_test.go +++ b/mail/smtp_sender_test.go @@ -28,18 +28,19 @@ func TestSendPlain(t *testing.T) { smtpServer.Clear() r := require.New(t) - m := mail.Message{ - From: "mark@example.com", - To: []string{"something@something.com"}, - Subject: "Cool Message", - CC: []string{"other@other.com", "my@other.com"}, - Bcc: []string{"secret@other.com"}, - } + m := mail.NewMessage() + m.From = "mark@example.com" + m.To = []string{"something@something.com"} + m.Subject = "Cool Message" + m.CC = []string{"other@other.com", "my@other.com"} + m.Bcc = []string{"secret@other.com"} 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) @@ -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}`) }