using the 'go-mail' framework (#50, #51)

This commit is contained in:
Michael Quigley 2022-09-12 17:02:51 -04:00
parent 0dae5d3118
commit de3412e3d1
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
5 changed files with 61 additions and 19 deletions

View File

@ -2,5 +2,5 @@ package email_ui
import "embed"
//go:embed *.gohtml
//go:embed verify.gohtml verify.gotext
var FS embed.FS

View File

@ -0,0 +1,5 @@
Welcome to zrok, {{ .EmailAddress }}!
Please visit this link to activate your zrok account:
{{ .VerifyUrl }}

View File

@ -2,12 +2,11 @@ package controller
import (
"bytes"
"fmt"
"github.com/openziti-test-kitchen/zrok/controller/email_ui"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/wneessen/go-mail"
"html/template"
"net/smtp"
)
type verificationEmail struct {
@ -16,29 +15,61 @@ type verificationEmail struct {
}
func sendVerificationEmail(emailAddress, token string, cfg *Config) error {
t, err := template.ParseFS(email_ui.FS, "verify.gohtml")
if err != nil {
return errors.Wrap(err, "error parsing email verification template")
}
buf := new(bytes.Buffer)
err = t.Execute(buf, &verificationEmail{
emailData := &verificationEmail{
EmailAddress: emailAddress,
VerifyUrl: cfg.Registration.RegistrationUrlTemplate + "/" + token,
})
if err != nil {
return errors.Wrap(err, "error executing email verification template")
}
subject := "Subject: Welcome to zrok!\n"
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
msg := []byte(subject + mime + buf.String())
auth := smtp.PlainAuth("", cfg.Email.Username, cfg.Email.Password, cfg.Email.Host)
to := []string{emailAddress}
err = smtp.SendMail(fmt.Sprintf("%v:%d", cfg.Email.Host, cfg.Email.Port), auth, cfg.Registration.EmailFrom, to, msg)
plainBody, err := mergeTemplate(emailData, "verify.gotext")
if err != nil {
return errors.Wrap(err, "error sending email verification")
return err
}
htmlBody, err := mergeTemplate(emailData, "verify.gohtml")
if err != nil {
return err
}
msg := mail.NewMsg()
if err := msg.From("ziggy@zrok.io"); err != nil {
return errors.Wrap(err, "failed to set from address in verification email")
}
if err := msg.To(emailAddress); err != nil {
return errors.Wrap(err, "failed to sent to address in verification email")
}
msg.Subject("Welcome to zrok!")
msg.SetDate()
msg.SetMessageID()
msg.SetBulk()
msg.SetImportance(mail.ImportanceHigh)
msg.SetBodyString(mail.TypeTextPlain, plainBody)
msg.SetBodyString(mail.TypeTextHTML, htmlBody)
client, err := mail.NewClient(cfg.Email.Host,
mail.WithPort(cfg.Email.Port),
mail.WithSMTPAuth(mail.SMTPAuthPlain),
mail.WithUsername(cfg.Email.Username),
mail.WithPassword(cfg.Email.Password),
mail.WithTLSPolicy(mail.TLSMandatory),
)
if err != nil {
return errors.Wrap(err, "error creating verification email client")
}
if err := client.DialAndSend(msg); err != nil {
return errors.Wrap(err, "error sending verification email")
}
logrus.Infof("verification email sent to '%v'", emailAddress)
return nil
}
func mergeTemplate(emailData *verificationEmail, filename string) (string, error) {
t, err := template.ParseFS(email_ui.FS, filename)
if err != nil {
return "", errors.Wrapf(err, "error parsing verification email template '%v'", filename)
}
buf := new(bytes.Buffer)
if err := t.Execute(buf, emailData); err != nil {
return "", errors.Wrapf(err, "error executing verification email template '%v'", filename)
}
return buf.String(), nil
}

2
go.mod
View File

@ -71,6 +71,8 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/valord577/mailx v0.3.20220208 // indirect
github.com/wneessen/go-mail v0.2.7 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.mongodb.org/mongo-driver v1.10.0 // indirect
go.mozilla.org/pkcs7 v0.0.0-20200128120323-432b2356ecb1 // indirect

4
go.sum
View File

@ -485,6 +485,10 @@ github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03O
github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk=
github.com/tklauser/numcpus v0.4.0 h1:E53Dm1HjH1/R2/aoCtXtPgzmElmn51aOkhCFSuZq//o=
github.com/tklauser/numcpus v0.4.0/go.mod h1:1+UI3pD8NW14VMwdgJNJ1ESk2UnwhAnz5hMwiKKqXCQ=
github.com/valord577/mailx v0.3.20220208 h1:h5B6DrWANVQBRQ5iz0fxwjaMkFIsF8Y4MhuKMyVZgs4=
github.com/valord577/mailx v0.3.20220208/go.mod h1:aGgPawsLOerLCxf7XU9jnke7B0lyp5Bl5JhsufWBwS0=
github.com/wneessen/go-mail v0.2.7 h1:4gj1flZjm05htmVj8AS6TbYXLQBYabzuQMmu8pZc/Js=
github.com/wneessen/go-mail v0.2.7/go.mod h1:m25lkU2GYQnlVr6tdwK533/UXxo57V0kLOjaFYmub0E=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs=
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=