Exemplo n.º 1
0
// NotifyEMail utilizes the Mandrill API to send a predefined template for the
// current build
func (n *NotifyEntry) NotifyEMail(metadata NotifyMetaData, cfg *config.Config) error {
	verb := "successful"
	if metadata.EventType == "error" {
		verb = "failed"
	}

	c := mandrill.ClientWithKey(cfg.Mandrill.MandrillAPIKey)
	message := &mandrill.Message{}
	message.AddRecipient(n.Target, "Gopher", "to")
	message.GlobalMergeVars = mandrill.MapToVars(map[string]interface{}{
		"REPO":      metadata.Repository,
		"STATE":     verb,
		"REPO_LINK": fmt.Sprintf("https://gobuilder.me/%s", metadata.Repository),
		"EMAIL":     n.Target,
	})
	message.AutoText = true
	message.TrackOpens = true
	message.TrackClicks = true
	message.InlineCSS = true

	templateContent := map[string]string{}
	_, err := c.MessagesSendTemplate(message, "gobuilder-status-mail", templateContent)

	return err
}
func (p *program) SendMail() {

	win := p.client.CheckWin(config.Config.Target)
	templateVars := map[string]string{}

	message := &mandrill.Message{}
	if win {
		message.Subject = "WIN --- Postcode Lottery"
	} else {
		message.Subject = "Postcode Lottery"
	}
	message.InlineCSS = true
	message.Subaccount = "fpl"

	message.FromEmail = config.Config.Services.Mandrill.Sender.Email
	message.FromName = config.Config.Services.Mandrill.Sender.Name

	message.AddRecipient(config.Config.Target, config.Config.Target, "to")

	if p.daily.Changed() {

		att := &mandrill.Attachment{
			Type:    "image/png",
			Name:    "daily",
			Content: p.daily.GetEncodedImage(),
		}
		templateVars["daily"] = "CHANGED"
		message.Attachments = make([]*mandrill.Attachment, 1)
		message.Attachments[0] = att
	}

	// Global vars

	if p.stockpot.Changed() {
		templateVars["stockpot"] = strings.Join(p.stockpot.GetPostcodes(), "<br>")
	}
	if p.survey.Changed() {
		templateVars["survey"] = p.survey.GetPostcode()
	}
	message.GlobalMergeVars = mandrill.MapToVars(templateVars)

	templateContent := map[string]string{}
	responses, err := p.mailer.MessagesSendTemplate(message, "fpl", templateContent)

	if err != nil {
		rollbar.Error(rollbar.ERR, err)
		if config.Config.Debug {
			logger.Error(err)
		}
	}

	for _, response := range responses {
		if strings.EqualFold(response.Status, "rejected") || strings.EqualFold(response.Status, "invalid") {
			apiErrorField := &rollbar.Field{Name: "Response", Data: response}
			rollbar.Error(rollbar.ERR, fmt.Errorf("Email send failed"), apiErrorField)
			if config.Config.Debug {
				logger.Error(response)
			}
		}

	}
}