2022-09-19 22:13:47 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-01-18 20:05:10 +01:00
|
|
|
"html/template"
|
|
|
|
|
2023-01-17 17:23:26 +01:00
|
|
|
"github.com/openziti/zrok/build"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/controller/emailUi"
|
2022-09-19 22:13:47 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/wneessen/go-mail"
|
|
|
|
)
|
|
|
|
|
|
|
|
type verificationEmail struct {
|
|
|
|
EmailAddress string
|
|
|
|
VerifyUrl string
|
2023-01-17 17:23:26 +01:00
|
|
|
Version string
|
2022-09-19 22:13:47 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 19:20:47 +02:00
|
|
|
func sendVerificationEmail(emailAddress, token string) error {
|
2022-09-19 22:13:47 +02:00
|
|
|
emailData := &verificationEmail{
|
|
|
|
EmailAddress: emailAddress,
|
|
|
|
VerifyUrl: cfg.Registration.RegistrationUrlTemplate + "/" + token,
|
2023-01-17 17:23:26 +01:00
|
|
|
Version: build.String(),
|
2022-09-19 22:13:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
plainBody, err := mergeTemplate(emailData, "verify.gotext")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
htmlBody, err := mergeTemplate(emailData, "verify.gohtml")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := mail.NewMsg()
|
2022-11-11 16:50:17 +01:00
|
|
|
if err := msg.From(cfg.Registration.EmailFrom); err != nil {
|
2022-09-19 22:13:47 +02:00
|
|
|
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) {
|
2022-12-14 20:40:45 +01:00
|
|
|
t, err := template.ParseFS(emailUi.FS, filename)
|
2022-09-19 22:13:47 +02:00
|
|
|
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
|
|
|
|
}
|