[#275] added support to customize the default user email templates from the Admin UI
This commit is contained in:
117
mails/user.go
117
mails/user.go
@@ -1,8 +1,8 @@
|
||||
package mails
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/mail"
|
||||
"strings"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/mails/templates"
|
||||
@@ -10,46 +10,6 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tokens"
|
||||
)
|
||||
|
||||
func prepareUserEmailBody(
|
||||
app core.App,
|
||||
user *models.User,
|
||||
token string,
|
||||
actionUrl string,
|
||||
bodyTemplate string,
|
||||
) (string, error) {
|
||||
settings := app.Settings()
|
||||
|
||||
// replace action url placeholder params (if any)
|
||||
actionUrlParams := map[string]string{
|
||||
core.EmailPlaceholderAppUrl: settings.Meta.AppUrl,
|
||||
core.EmailPlaceholderToken: token,
|
||||
}
|
||||
for k, v := range actionUrlParams {
|
||||
actionUrl = strings.ReplaceAll(actionUrl, k, v)
|
||||
}
|
||||
var urlErr error
|
||||
actionUrl, urlErr = normalizeUrl(actionUrl)
|
||||
if urlErr != nil {
|
||||
return "", urlErr
|
||||
}
|
||||
|
||||
params := struct {
|
||||
AppName string
|
||||
AppUrl string
|
||||
User *models.User
|
||||
Token string
|
||||
ActionUrl string
|
||||
}{
|
||||
AppName: settings.Meta.AppName,
|
||||
AppUrl: settings.Meta.AppUrl,
|
||||
User: user,
|
||||
Token: token,
|
||||
ActionUrl: actionUrl,
|
||||
}
|
||||
|
||||
return resolveTemplateContent(params, templates.Layout, bodyTemplate)
|
||||
}
|
||||
|
||||
// SendUserPasswordReset sends a password reset request email to the specified user.
|
||||
func SendUserPasswordReset(app core.App, user *models.User) error {
|
||||
token, tokenErr := tokens.NewUserResetPasswordToken(app, user)
|
||||
@@ -66,24 +26,20 @@ func SendUserPasswordReset(app core.App, user *models.User) error {
|
||||
}
|
||||
|
||||
sendErr := app.OnMailerBeforeUserResetPasswordSend().Trigger(event, func(e *core.MailerUserEvent) error {
|
||||
body, err := prepareUserEmailBody(
|
||||
app,
|
||||
user,
|
||||
token,
|
||||
app.Settings().Meta.UserResetPasswordUrl,
|
||||
templates.UserPasswordResetBody,
|
||||
)
|
||||
settings := app.Settings()
|
||||
|
||||
subject, body, err := resolveEmailTemplate(app, token, settings.Meta.ResetPasswordTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return e.MailClient.Send(
|
||||
mail.Address{
|
||||
Name: app.Settings().Meta.SenderName,
|
||||
Address: app.Settings().Meta.SenderAddress,
|
||||
Name: settings.Meta.SenderName,
|
||||
Address: settings.Meta.SenderAddress,
|
||||
},
|
||||
mail.Address{Address: e.User.Email},
|
||||
("Reset your " + app.Settings().Meta.AppName + " password"),
|
||||
subject,
|
||||
body,
|
||||
nil,
|
||||
)
|
||||
@@ -112,24 +68,20 @@ func SendUserVerification(app core.App, user *models.User) error {
|
||||
}
|
||||
|
||||
sendErr := app.OnMailerBeforeUserVerificationSend().Trigger(event, func(e *core.MailerUserEvent) error {
|
||||
body, err := prepareUserEmailBody(
|
||||
app,
|
||||
user,
|
||||
token,
|
||||
app.Settings().Meta.UserVerificationUrl,
|
||||
templates.UserVerificationBody,
|
||||
)
|
||||
settings := app.Settings()
|
||||
|
||||
subject, body, err := resolveEmailTemplate(app, token, settings.Meta.VerificationTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return e.MailClient.Send(
|
||||
mail.Address{
|
||||
Name: app.Settings().Meta.SenderName,
|
||||
Address: app.Settings().Meta.SenderAddress,
|
||||
Name: settings.Meta.SenderName,
|
||||
Address: settings.Meta.SenderAddress,
|
||||
},
|
||||
mail.Address{Address: e.User.Email},
|
||||
("Verify your " + app.Settings().Meta.AppName + " email"),
|
||||
subject,
|
||||
body,
|
||||
nil,
|
||||
)
|
||||
@@ -161,24 +113,20 @@ func SendUserChangeEmail(app core.App, user *models.User, newEmail string) error
|
||||
}
|
||||
|
||||
sendErr := app.OnMailerBeforeUserChangeEmailSend().Trigger(event, func(e *core.MailerUserEvent) error {
|
||||
body, err := prepareUserEmailBody(
|
||||
app,
|
||||
user,
|
||||
token,
|
||||
app.Settings().Meta.UserConfirmEmailChangeUrl,
|
||||
templates.UserConfirmEmailChangeBody,
|
||||
)
|
||||
settings := app.Settings()
|
||||
|
||||
subject, body, err := resolveEmailTemplate(app, token, settings.Meta.ConfirmEmailChangeTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return e.MailClient.Send(
|
||||
mail.Address{
|
||||
Name: app.Settings().Meta.SenderName,
|
||||
Address: app.Settings().Meta.SenderAddress,
|
||||
Name: settings.Meta.SenderName,
|
||||
Address: settings.Meta.SenderAddress,
|
||||
},
|
||||
mail.Address{Address: newEmail},
|
||||
("Confirm your " + app.Settings().Meta.AppName + " new email address"),
|
||||
subject,
|
||||
body,
|
||||
nil,
|
||||
)
|
||||
@@ -190,3 +138,30 @@ func SendUserChangeEmail(app core.App, user *models.User, newEmail string) error
|
||||
|
||||
return sendErr
|
||||
}
|
||||
|
||||
func resolveEmailTemplate(
|
||||
app core.App,
|
||||
token string,
|
||||
emailTemplate core.EmailTemplate,
|
||||
) (subject string, body string, err error) {
|
||||
settings := app.Settings()
|
||||
|
||||
subject, rawBody, _ := emailTemplate.Resolve(
|
||||
settings.Meta.AppName,
|
||||
settings.Meta.AppUrl,
|
||||
token,
|
||||
)
|
||||
|
||||
params := struct {
|
||||
HtmlContent template.HTML
|
||||
}{
|
||||
HtmlContent: template.HTML(rawBody),
|
||||
}
|
||||
|
||||
body, err = resolveTemplateContent(params, templates.Layout, templates.HtmlBody)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
return subject, body, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user