示例#1
0
// GetAccount gets the account which sent the message by email of user
func GetAccount(mailAddress string) (*mongomodels.Account, error) {
	user, err := modelhelper.FetchUserByEmail(mailAddress)
	if err != nil {
		return nil, ErrEmailIsNotFetched
	}

	// This GetAccount
	account, err := modelhelper.GetAccount(user.Name)
	if err != nil {
		return nil, ErrAccountIsNotFound
	}
	return account, nil
}
示例#2
0
func (m *MailgunSender) SendMailgunEmail(mail *Mail) error {
	var subject string
	var email string
	var err error
	var userId string
	var tpl *template.Template
	var tplText *texttemplate.Template
	userObj := EmailInvitationUser{}

	if mail.Properties.Options["subject"] == keyInvitedCreateTeam {
		email = mail.Properties.Options["invitee"].(string)
		userObj.UserID = email
		userObj.Link = mail.Properties.Options["link"].(string)
		subject = subjectInvitedCreateTeam
		tpl = m.TemplateTeamInvite
		tplText = m.TemplateTeamInviteText
	} else {
		return errors.New("Wrong subject in mailgun.go")
	}

	user, err := modelhelper.FetchUserByEmail(email)
	if err == nil {
		userId = user.ObjectId.Hex()
	} else {
		userId = "0"
	}

	if user.EmailFrequency != nil && !user.EmailFrequency.Global {
		return errors.New("User is unsubscribed from all emails")
	}

	userObj.LinkUnsubscribe = fmt.Sprintf("%s/Unsubscribe/%s/%s", m.VmHostname, userId, email)

	buf := new(bytes.Buffer)
	err = tpl.Execute(buf, userObj)
	if err != nil {
		m.Log.Error("Sending email template execute err: %s", err)
		return err
	}

	bufText := new(bytes.Buffer)
	err = tplText.Execute(bufText, userObj)

	if err != nil {
		m.Log.Error("Sending email template execute err: %s", err)
		return err
	}

	message := mailgun.NewMessage(
		emailFrom,
		subject,
		bufText.String(),
		email)

	message.SetHtml(buf.String())
	_, _, err = m.Mailgun.Send(message)
	if err != nil {
		m.Log.Error("Sending email err: %s", err)
		return err
	}

	return err
}