Use html in emails

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-10-14 16:30:33 +02:00
parent 00c0a56f50
commit ae45410b82
34 changed files with 2211 additions and 208 deletions

View File

@@ -15,16 +15,14 @@
package auth
import (
"bytes"
"context"
_ "embed"
"errors"
"fmt"
"html/template"
"net/mail"
"net/url"
"time"
"github.com/getprobo/probo/packages/emails"
"github.com/getprobo/probo/pkg/coredata"
"github.com/getprobo/probo/pkg/crypto/passwdhash"
"github.com/getprobo/probo/pkg/gid"
@@ -100,18 +98,6 @@ const (
TokenTypePasswordReset = "password_reset"
)
var (
//go:embed emails/signup_confirmation.txt.tmpl
signupEmailTemplateData string
signupEmailTemplate = template.Must(template.New("signup").Parse(signupEmailTemplateData))
signupEmailSubject = "Confirm your email address"
//go:embed emails/password_reset.txt.tmpl
passwordResetEmailTemplateData string
passwordResetEmailTemplate = template.Must(template.New("password_reset").Parse(passwordResetEmailTemplateData))
passwordResetEmailSubject = "Reset your password"
)
func (e ErrInvalidCredentials) Error() string {
return e.message
}
@@ -204,23 +190,20 @@ func (s Service) ForgetPassword(
return fmt.Errorf("cannot load user: %w", err)
}
body := bytes.NewBuffer(nil)
err = passwordResetEmailTemplate.Execute(
body,
map[string]string{
"FullName": user.FullName,
"ResetURL": resetPasswordUrl.String(),
},
subject, textBody, htmlBody, err := emails.RenderPasswordReset(
user.FullName,
resetPasswordUrl.String(),
)
if err != nil {
return fmt.Errorf("cannot execute password reset template: %w", err)
return fmt.Errorf("cannot render password reset email: %w", err)
}
passwordResetEmail := coredata.NewEmail(
user.FullName,
email,
passwordResetEmailSubject,
body.String(),
subject,
textBody,
htmlBody,
)
if err := passwordResetEmail.Insert(ctx, conn); err != nil {
return fmt.Errorf("cannot insert email: %w", err)
@@ -308,23 +291,20 @@ func (s Service) SignUp(
}.Encode(),
}
body := bytes.NewBuffer(nil)
err = signupEmailTemplate.Execute(
body,
map[string]string{
"FullName": user.FullName,
"ConfirmationURL": confirmationUrl.String(),
},
subject, textBody, htmlBody, err := emails.RenderConfirmEmail(
user.FullName,
confirmationUrl.String(),
)
if err != nil {
return fmt.Errorf("cannot execute signup template: %w", err)
return fmt.Errorf("cannot render confirmation email: %w", err)
}
confirmationEmail := coredata.NewEmail(
user.FullName,
user.EmailAddress,
signupEmailSubject,
body.String(),
subject,
textBody,
htmlBody,
)
if err := confirmationEmail.Insert(ctx, tx); err != nil {