Stripped down package for sending emails through the Mandrill API. Inspired by @mostafah's implementation.
go get -u github.com/keighl/mandrill
MessagesSend()
and MessagesSendTemplate()
now only returns 1 error interface (as opposed to a non-sensical 2).
// New!
res, err := client.MessagesSend(m)
// Old!
res, apiError, err := client.MessagesSend(m)
http://godoc.org/github.com/keighl/mandrill
https://mandrillapp.com/api/docs/messages.JSON.html#method=send
import (
m "github.com/keighl/mandrill"
)
client := m.ClientWithKey("XXXXXXXXX")
message := &m.Message{}
message.AddRecipient("bob@example.com", "Bob Johnson", "to")
message.FromEmail = "kyle@example.com"
message.FromName = "Kyle Truscott"
message.Subject = "You won the prize!"
message.HTML = "<h1>You won!!</h1>"
message.Text = "You won!!"
responses, err := client.MessagesSend(message)
https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template
templateContent := map[string]string{"header": "Bob! You won the prize!"}
responses, err := client.MessagesSendTemplate(message, "you-won", templateContent)
http://help.mandrill.com/entries/21678522-How-do-I-use-merge-tags-to-add-dynamic-content-
// Global vars
message.GlobalMergeVars = m.MapToVars(map[string]interface{}{"name": "Bob"})
// Recipient vars
bobVars := m.MapToRecipientVars("bob@example.com", map[string]interface{}{"name": "Bob"})
jillVars := m.MapToRecipientVars("jill@example.com", map[string]interface{}{"name": "Jill"})
message.MergeVars = []*m.RcptMergeVars{bobVars, jillVars}
You can pass special API keys to the client to mock success/err responses from MessagesSend
or MessagesSendTemplate
.
// Sending messages will be successful, but without a real API request
c := ClientWithKey("SANDBOX_SUCCESS")
// Sending messages will error, but without a real API request
c := ClientWithKey("SANDBOX_ERROR")