예제 #1
0
func (mgp *MailGunProvider) mgSend(m model.Email) {

	from := m.GetHeader(model.EML_HDR_FROM)
	to := m.GetHeader(model.EML_HDR_TO)
	subject := m.GetHeader(model.EML_HDR_SUBJECT)
	content := m.GetContent()

	message := mgp.mg.NewMessage(from, subject, content, to)

	mgp.log.Println("Invoking MailGun API to send e-mail")
	var mm, mailId, err = mgp.mg.Send(message)

	// report MailGunProvider as down
	if err != nil {
		mgp.log.Println("MG Failed to send e-mail")
		mgp.log.Println(err.Error())
		if mgp.failureStrategy.Failure(mtacontainer.EK_CRITICAL) == false {
			mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_WARNING, err, mgp)
			mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, err, m)
		} else {
			mgp.Stop() // we are officially going down
			mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_FATAL, err)
			mgp.log.Println("The MailGun Provider is considered Down.")
			mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, errors.New("MailGun is down for sending"), m)
			for e := range mgp.out {
				var ee model.Email = e
				mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, errors.New("MailGun is down for sending"), ee)
			}

		}
	} else {
		mgp.log.Println("MG Has sent email with success.")
		mgp.failureStrategy.Success()
		mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_BEAT,
			errors.New("MailGun says: "+mm+" for sending message giving it id "+mailId))
		mgp.log.Println("After sending event on event channel")
		mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_INFORM_USER,
			errors.New("Mail Delivered With MailGun Successfully"), m.GetSessionId())

	}
}
예제 #2
0
func (ths *SendGridProvider) sgSend(m model.Email) {

	message := sendgrid.NewMail()
	message.From = m.GetHeader(model.EML_HDR_FROM)
	message.To = m.GetHeaders()[model.EML_HDR_TO]
	message.Subject = m.GetHeader(model.EML_HDR_SUBJECT)
	for k, _ := range m.GetHeaders() {
		if strings.Compare(k, model.EML_HDR_FROM) != 0 &&
			strings.Compare(k, model.EML_HDR_TO) != 0 &&
			strings.Compare(k, model.EML_HDR_SUBJECT) != 0 {
			message.AddHeader(k, m.GetHeader(k))
		}
	}
	message.SetText(m.GetContent())

	err := ths.sg.Send(message)

	// report SendGrid Provider as down
	if err != nil {
		ths.log.Println(err.Error())
		if ths.failureStrategy.Failure(mtacontainer.EK_CRITICAL) == false {
			ths.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, errors.New("Sendgrid resubmits mail"), m)
		} else {
			ths.Stop() // we are officially going down
			ths.events <- mtacontainer.NewEvent(mtacontainer.EK_FATAL, err)
			ths.log.Println("The SendGrid Provider is considered Down.")
			ths.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, errors.New("SendGrid is down for sending"), m)
			for e := range ths.outgoing {
				var ee model.Email = e
				ths.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, errors.New("SendGrid is down for sending"), ee)
			}

		}
	} else {
		ths.failureStrategy.Success()
		ths.events <- mtacontainer.NewEvent(mtacontainer.EK_BEAT,
			errors.New("SendGrid send a message"))
		ths.events <- mtacontainer.NewEvent(mtacontainer.EK_INFORM_USER, errors.New("Mail Delivered With SendGrid Successfully"), m.GetSessionId())

	}
}