From 2da5b12c31074591eaf16929b760322b98f189e8 Mon Sep 17 00:00:00 2001 From: jloup Date: Fri, 11 May 2018 13:57:29 +0200 Subject: Mails. --- api/mail.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 api/mail.go (limited to 'api/mail.go') diff --git a/api/mail.go b/api/mail.go new file mode 100644 index 0000000..e0f6ccb --- /dev/null +++ b/api/mail.go @@ -0,0 +1,103 @@ +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) +} -- cgit v1.2.3