func (d *SendGridDriver) Send(language, defaultLang string, t *pb.Target, temp *template.TemplateGroup) error {
	m := t.GetEmail()

	message := sendgrid.NewMail()

	if len(m.FromEmail) > 0 {
		message.SetFrom(m.FromEmail)
	} else {
		message.SetFrom(d.DefaultFromEmail)
	}

	message.AddTos(m.ToEmail)
	message.AddCcs(m.Cc)
	message.AddBccs(m.Bcc)

	if len(m.ReplyTo) > 0 {
		message.SetReplyTo(m.ReplyTo)
	}
	if len(m.ToName) > 0 {
		message.AddToNames(m.ToName)
	}
	if len(m.FromName) > 0 {
		message.SetFromName(m.FromName)
	} else {
		message.SetFromName(d.DefaultFromName)
	}

	if txt := temp.GetText(template.TemplateHtml, language, defaultLang, m.Data); len(txt) > 0 {
		message.SetHTML(txt)
	}

	if txt := temp.GetText(template.TemplateText, language, defaultLang, m.Data); len(txt) > 0 {
		message.SetText(txt)
	}

	if txt := temp.GetText(template.TemplateEmailSubject, language, defaultLang, m.Data); len(txt) > 0 {
		message.SetSubject(txt)
	} else {
		message.SetSubject(m.Subject)
	}

	r := d.Client.Send(message)
	if r != nil {
		log.Errorln(r)
	}
	return r
}
Esempio n. 2
0
func (s *SendgridExporter) Send(event *Event) error {
	var err error

	mail := sendgrid.NewMail()
	if mail, err = setTo(mail, event); err != nil {
		return err
	}

	if mail, err = setFrom(mail, event); err != nil {
		return err
	}

	if mail, err = setBody(mail, event); err != nil {
		return err
	}

	if mail, err = setSubject(mail, event); err != nil {
		return err
	}

	return s.Client.Send(mail)
}