Skip to content

Commit

Permalink
Mandrill tests, mocking, minor edits
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jun 22, 2020
1 parent 2330698 commit 6b84c25
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 75 deletions.
10 changes: 5 additions & 5 deletions config.go
Expand Up @@ -54,11 +54,11 @@ type MailService struct {
TrackOpens bool `json:"track_opens" mapstructure:"track_opens"` // whether or not to turn on open tracking for the message

// awsSesService awsSesInterface // AWS SES client
awsSesService ses.Config // AWS SES client
mandrillService *gochimp.MandrillAPI // Mandrill api client
postmarkService postmarkInterface // Postmark api client
smtpAuth smtp.Auth // Auth credentials for SMTP
smtpClient smtpInterface // SMTP client
awsSesService ses.Config // AWS SES client
mandrillService mandrillInterface // Mandrill api client
postmarkService postmarkInterface // Postmark api client
smtpAuth smtp.Auth // Auth credentials for SMTP
smtpClient smtpInterface // SMTP client
}

// containsServiceProvider is a simple lookup for a service provider in a list of providers
Expand Down
2 changes: 1 addition & 1 deletion email.go
Expand Up @@ -181,7 +181,7 @@ func (m *MailService) SendEmail(email *Email, provider ServiceProvider) (err err
if provider == AwsSes {
err = sendViaAwsSes(&m.awsSesService, email)
} else if provider == Mandrill {
err = m.sendWithMandrill(email)
err = sendViaMandrill(m.mandrillService, email, false)
} else if provider == Postmark {
err = sendViaPostmark(m.postmarkService, email)
} else if provider == SMTP {
Expand Down
43 changes: 21 additions & 22 deletions examples/examples.go
Expand Up @@ -14,10 +14,10 @@ import (
func main() {

// Run the Mandrill example
// mandrillExample()
mandrillExample()

// Run the Postmark example
postmarkExample()
// postmarkExample()

// Run the SMTP example
// smtpExample()
Expand Down Expand Up @@ -57,7 +57,7 @@ func mandrillExample() {
// Start the service
err := mail.StartUp()
if err != nil {
log.Printf("error in StartUp: %s using provider %x", err.Error(), provider)
log.Printf("error in StartUp: %s using provider: %x", err.Error(), provider)
}

// Create and send a basic email
Expand All @@ -67,9 +67,8 @@ func mandrillExample() {
email.Subject = "example go-mail email using Mandrill"

// Send the email
err = mail.SendEmail(email, provider)
if err != nil {
log.Fatalf("error in SendEmail: %s using provider %x", err.Error(), provider)
if err = mail.SendEmail(email, provider); err != nil {
log.Fatalf("error in SendEmail: %s using provider: %x", err.Error(), provider)
}
log.Printf("email sent!")
}
Expand Down Expand Up @@ -102,7 +101,7 @@ func postmarkExample() {
// Start the service
err := mail.StartUp()
if err != nil {
log.Printf("error in StartUp: %s using provider %x", err.Error(), provider)
log.Printf("error in StartUp: %s using provider: %x", err.Error(), provider)
}

// Create and send a basic email
Expand All @@ -112,9 +111,8 @@ func postmarkExample() {
email.Subject = "example go-mail email using Postmark"

// Send the email
err = mail.SendEmail(email, provider)
if err != nil {
log.Fatalf("error in SendEmail: %s using provider %x", err.Error(), provider)
if err = mail.SendEmail(email, provider); err != nil {
log.Fatalf("error in SendEmail: %s using provider: %x", err.Error(), provider)
}
log.Printf("email sent!")
}
Expand Down Expand Up @@ -156,7 +154,7 @@ func smtpExample() {
// Start the service
err := mail.StartUp()
if err != nil {
log.Printf("error in StartUp: %s using provider %x", err.Error(), provider)
log.Printf("error in StartUp: %s using provider: %x", err.Error(), provider)
}

// Create and send a basic email
Expand All @@ -166,9 +164,8 @@ func smtpExample() {
email.Subject = "example go-mail email using SMTP"

// Send the email
err = mail.SendEmail(email, provider)
if err != nil {
log.Fatalf("error in SendEmail: %s using provider %x", err.Error(), provider)
if err = mail.SendEmail(email, provider); err != nil {
log.Fatalf("error in SendEmail: %s using provider: %x", err.Error(), provider)
}
log.Printf("email sent!")
}
Expand Down Expand Up @@ -205,7 +202,7 @@ func awsSesExample() {
// Start the service
err := mail.StartUp()
if err != nil {
log.Printf("error in StartUp: %s using provider %x", err.Error(), provider)
log.Printf("error in StartUp: %s using provider: %x", err.Error(), provider)
}

// Create and send a basic email
Expand All @@ -215,9 +212,8 @@ func awsSesExample() {
email.Subject = "example go-mail email using AWS SES"

// Send the email
err = mail.SendEmail(email, provider)
if err != nil {
log.Fatalf("error in SendEmail: %s using provider %x", err.Error(), provider)
if err = mail.SendEmail(email, provider); err != nil {
log.Fatalf("error in SendEmail: %s using provider: %x", err.Error(), provider)
}
log.Printf("email sent!")
}
Expand Down Expand Up @@ -247,12 +243,12 @@ func allOptionsExample() {
mail.SMTPUsername = os.Getenv("EMAIL_SMTP_USERNAME") // johndoe
mail.SMTPPassword = os.Getenv("EMAIL_SMTP_PASSWORD") // secretPassword

provider := gomail.SMTP // AwsSes Mandrill Postmark
provider := gomail.SMTP // Other options: AwsSes Mandrill Postmark

// Start the service
err := mail.StartUp()
if err != nil {
log.Printf("error in StartUp: %s using provider %x", err.Error(), provider)
log.Printf("error in StartUp: %s using provider: %x", err.Error(), provider)
}

// Available services given the config above
Expand All @@ -269,6 +265,9 @@ func allOptionsExample() {
email.Subject = "testing go-mail - example email"
email.Tags = []string{"admin_alert"}
email.Important = true
email.TrackClicks = true
email.TrackOpens = true
email.AutoText = true

// Add an attachment
var f *os.File
Expand All @@ -281,9 +280,9 @@ func allOptionsExample() {

// Send the email (basic example using one provider)
if err = mail.SendEmail(email, provider); err != nil {
log.Fatalf("error in SendEmail: %s using provider %x", err.Error(), provider)
log.Fatalf("error in SendEmail: %s using provider: %x", err.Error(), provider)
}

// Congrats!
log.Printf("all emails sent~")
log.Printf("all emails sent!")
}
93 changes: 47 additions & 46 deletions mandrill.go
Expand Up @@ -10,33 +10,40 @@ import (
"github.com/mattbaird/gochimp"
)

// sendWithMandrill sends an email using the Mandrill service
func (m *MailService) sendWithMandrill(email *Email) (err error) {
// mandrillInterface is an interface for Mandrill/mocking
type mandrillInterface interface {
MessageSend(message gochimp.Message, async bool) ([]gochimp.SendResponse, error)
}

// sendViaMandrill sends an email using the Mandrill service
// Mandrill uses the word Message for their email
func sendViaMandrill(client mandrillInterface, email *Email, async bool) (err error) {

// Get the signing domain from the from address
sign := strings.Split(email.FromAddress, "@")
var signDomain string
if len(sign) <= 1 {
signDomain = m.FromDomain
} else {
signDomain = sign[1]
err = fmt.Errorf("invalid from address, sign domain not found using %s", email.FromAddress)
return
}

// Create the Mandrill Email
// Set the domain using the FromAddress
signDomain := sign[1]

// Create the Mandrill email
message := gochimp.Message{
AutoText: email.AutoText,
FromEmail: email.FromAddress,
FromName: email.FromName,
Html: email.HTMLContent,
Important: email.Important,
PreserveRecipients: false,
SigningDomain: signDomain,
Subject: email.Subject,
Tags: email.Tags,
Text: email.PlainTextContent,
Important: email.Important,
TrackOpens: email.TrackOpens,
TrackClicks: email.TrackClicks,
TrackOpens: email.TrackOpens,
ViewContentLink: email.ViewContentLink,
AutoText: email.AutoText,
}

// Convert recipients
Expand All @@ -49,55 +56,49 @@ func (m *MailService) sendWithMandrill(email *Email) (err error) {
}

// Convert any BCC recipients
if len(email.RecipientsBcc) > 0 {
for _, recipient := range email.RecipientsBcc {
emailRecipient := gochimp.Recipient{
Email: recipient,
Type: "bcc",
}
message.To = append(message.To, emailRecipient)
for _, recipient := range email.RecipientsBcc {
emailRecipient := gochimp.Recipient{
Email: recipient,
Type: "bcc",
}
message.To = append(message.To, emailRecipient)
}

// Convert any BCC recipients
if len(email.RecipientsCc) > 0 {
for _, recipient := range email.RecipientsCc {
emailRecipient := gochimp.Recipient{
Email: recipient,
Type: "cc",
}
message.To = append(message.To, emailRecipient)
// Convert any CC recipients
for _, recipient := range email.RecipientsCc {
emailRecipient := gochimp.Recipient{
Email: recipient,
Type: "cc",
}
message.To = append(message.To, emailRecipient)
}

// Convert attachments to Mandrill format
if len(email.Attachments) > 0 {
for _, attachment := range email.Attachments {

// Create the mandrill attachment
mandrillAttachment := new(gochimp.Attachment)
mandrillAttachment.Name = attachment.FileName
mandrillAttachment.Type = attachment.FileType

// Read all content from the attachment
reader := bufio.NewReader(attachment.FileReader)
var content []byte
if content, err = ioutil.ReadAll(reader); err != nil {
return
}
for _, attachment := range email.Attachments {

// Encode as base64
encoded := base64.StdEncoding.EncodeToString(content)
mandrillAttachment.Content = encoded
// Create the Mandrill attachment
mandrillAttachment := &gochimp.Attachment{
Name: attachment.FileName,
Type: attachment.FileType,
}

// Add to the email
message.Attachments = append(message.Attachments, *mandrillAttachment)
// Read all content from the attachment
reader := bufio.NewReader(attachment.FileReader)
var content []byte
if content, err = ioutil.ReadAll(reader); err != nil {
return
}

// Encode as base64
mandrillAttachment.Content = base64.StdEncoding.EncodeToString(content)

// Add to the email
message.Attachments = append(message.Attachments, *mandrillAttachment)
}

// Execute the send
// Send the email
var sendResponse []gochimp.SendResponse
if sendResponse, err = m.mandrillService.MessageSend(message, false); err != nil {
if sendResponse, err = client.MessageSend(message, async); err != nil {
return
}

Expand Down

0 comments on commit 6b84c25

Please sign in to comment.