more verification email elaboration (#50, #51)

This commit is contained in:
Michael Quigley 2022-09-12 15:00:44 -04:00
parent 600f0396d2
commit 712bdf734b
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,6 @@
package email_ui
import "embed"
//go:embed *.gohtml
var FS embed.FS

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome to zrok!</title>
</head>
<body>
<h1>Welcome to <code>zrok</code>, {{ .EmailAddress }}!</h1>
<p>Please click this <a href="{{ .VerifyUrl }}">link</a> to create your <code>zrok</code> account.</p>
</body>
</html>

41
controller/verify.go Normal file
View File

@ -0,0 +1,41 @@
package controller
import (
"bytes"
"fmt"
"github.com/openziti-test-kitchen/zrok/controller/email_ui"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"html/template"
"net/smtp"
)
type verificationEmail struct {
EmailAddress string
VerifyUrl string
}
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{
EmailAddress: emailAddress,
VerifyUrl: cfg.Registration.RegistrationUrlTemplate + "/" + token,
})
if err != nil {
return errors.Wrap(err, "error executing email verification template")
}
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, buf.Bytes())
if err != nil {
return errors.Wrap(err, "error sending email verification")
}
logrus.Infof("verification email sent to '%v'", emailAddress)
return nil
}