package api
import (
"fmt"
"strings"
"github.com/matcornic/hermes"
"gopkg.in/gomail.v2"
)
var MailTemplateEngine hermes.Hermes
func ConfigureMailTemplateEngine() {
var link string
if strings.Contains(CONFIG.Domain, "localhost") {
link = fmt.Sprintf("http://%s", CONFIG.Domain)
} else {
link = fmt.Sprintf("https://%s", CONFIG.Domain)
}
MailTemplateEngine = hermes.Hermes{
Product: hermes.Product{
Name: "CryptoPF",
Link: link,
Copyright: "Copyright © 2017 CryptoPF. All rights reserved.",
},
}
}
func SendResetPasswordMail(to, token string) error {
mail := hermes.Email{
Body: hermes.Body{
Name: to,
Intros: []string{
"You have received this email because a password reset request for your CryptoPF account was received.",
},
Actions: []hermes.Action{
{
Instructions: "Click the button below to reset your password:",
Button: hermes.Button{
Color: "#DC4D2F",
Text: "Reset your password",
Link: fmt.Sprintf("%s/change-password?token=%s", MailTemplateEngine.Product.Link, token),
},
},
},
Outros: []string{
"If you did not request a password reset, no further action is required on your part.",
},
Signature: "Thanks",
},
}
body, err := MailTemplateEngine.GenerateHTML(mail)
if err != nil {
return err
}
return SendEmail(to, "Password reset", body)
}
func SendConfirmationMail(to, token string) error {
mail := hermes.Email{
Body: hermes.Body{
Name: to,
Intros: []string{
"Welcome to CryptoPF! We're very excited to have you on board.",
},
Actions: []hermes.Action{
{
Instructions: "To get started with CryptoPF, please click here:",
Button: hermes.Button{
Text: "Confirm your account",
Link: fmt.Sprintf("%s/confirm?token=%s", MailTemplateEngine.Product.Link, token),
},
},
},
Outros: []string{
"Need help, or have questions? Just reply to this email, we'd love to help.",
},
Signature: "Thanks",
},
}
body, err := MailTemplateEngine.GenerateHTML(mail)
if err != nil {
return err
}
return SendEmail(to, "Confirm your email", body)
}
func SendEmail(to, subject, body string) error {
m := gomail.NewMessage()
m.SetAddressHeader("From", MAIL_CONFIG.AddressFrom, "CryptoPF")
m.SetAddressHeader("To", to, to)
m.SetHeader("Subject", subject)
m.SetBody("text/html", body)
d := gomail.NewPlainDialer(MAIL_CONFIG.SmtpAddress, 587, MAIL_CONFIG.Login, MAIL_CONFIG.Password)
return d.DialAndSend(m)
}