2022-07-06 23:19:05 +02:00
|
|
|
package mails
|
|
|
|
|
|
|
|
import (
|
2022-08-14 18:30:45 +02:00
|
|
|
"html/template"
|
2022-07-06 23:19:05 +02:00
|
|
|
"net/mail"
|
|
|
|
|
|
|
|
"github.com/pocketbase/pocketbase/core"
|
|
|
|
"github.com/pocketbase/pocketbase/mails/templates"
|
|
|
|
"github.com/pocketbase/pocketbase/models"
|
2022-11-26 14:42:45 +02:00
|
|
|
"github.com/pocketbase/pocketbase/models/settings"
|
2022-07-06 23:19:05 +02:00
|
|
|
"github.com/pocketbase/pocketbase/tokens"
|
2022-11-21 14:53:05 +02:00
|
|
|
"github.com/pocketbase/pocketbase/tools/mailer"
|
2022-07-06 23:19:05 +02:00
|
|
|
)
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
// SendRecordPasswordReset sends a password reset request email to the specified user.
|
|
|
|
func SendRecordPasswordReset(app core.App, authRecord *models.Record) error {
|
|
|
|
token, tokenErr := tokens.NewRecordResetPasswordToken(app, authRecord)
|
2022-07-06 23:19:05 +02:00
|
|
|
if tokenErr != nil {
|
|
|
|
return tokenErr
|
|
|
|
}
|
|
|
|
|
|
|
|
mailClient := app.NewMailClient()
|
|
|
|
|
2022-11-26 14:42:45 +02:00
|
|
|
subject, body, err := resolveEmailTemplate(app, token, app.Settings().Meta.ResetPasswordTemplate)
|
2022-11-21 14:53:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
message := &mailer.Message{
|
|
|
|
From: mail.Address{
|
2022-11-26 14:42:45 +02:00
|
|
|
Name: app.Settings().Meta.SenderName,
|
|
|
|
Address: app.Settings().Meta.SenderAddress,
|
2022-11-21 14:53:05 +02:00
|
|
|
},
|
|
|
|
To: mail.Address{Address: authRecord.Email()},
|
|
|
|
Subject: subject,
|
|
|
|
HTML: body,
|
|
|
|
}
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
event := &core.MailerRecordEvent{
|
2022-07-06 23:19:05 +02:00
|
|
|
MailClient: mailClient,
|
2022-11-21 14:53:05 +02:00
|
|
|
Message: message,
|
2022-10-30 10:28:14 +02:00
|
|
|
Record: authRecord,
|
2022-07-06 23:19:05 +02:00
|
|
|
Meta: map[string]any{"token": token},
|
|
|
|
}
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
sendErr := app.OnMailerBeforeRecordResetPasswordSend().Trigger(event, func(e *core.MailerRecordEvent) error {
|
2022-11-21 14:53:05 +02:00
|
|
|
return e.MailClient.Send(e.Message)
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if sendErr == nil {
|
2022-10-30 10:28:14 +02:00
|
|
|
app.OnMailerAfterRecordResetPasswordSend().Trigger(event)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sendErr
|
|
|
|
}
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
// SendRecordVerification sends a verification request email to the specified user.
|
|
|
|
func SendRecordVerification(app core.App, authRecord *models.Record) error {
|
|
|
|
token, tokenErr := tokens.NewRecordVerifyToken(app, authRecord)
|
2022-07-06 23:19:05 +02:00
|
|
|
if tokenErr != nil {
|
|
|
|
return tokenErr
|
|
|
|
}
|
|
|
|
|
|
|
|
mailClient := app.NewMailClient()
|
|
|
|
|
2022-11-26 14:42:45 +02:00
|
|
|
subject, body, err := resolveEmailTemplate(app, token, app.Settings().Meta.VerificationTemplate)
|
2022-11-21 14:53:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
message := &mailer.Message{
|
|
|
|
From: mail.Address{
|
2022-11-26 14:42:45 +02:00
|
|
|
Name: app.Settings().Meta.SenderName,
|
|
|
|
Address: app.Settings().Meta.SenderAddress,
|
2022-11-21 14:53:05 +02:00
|
|
|
},
|
|
|
|
To: mail.Address{Address: authRecord.Email()},
|
|
|
|
Subject: subject,
|
|
|
|
HTML: body,
|
|
|
|
}
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
event := &core.MailerRecordEvent{
|
2022-07-06 23:19:05 +02:00
|
|
|
MailClient: mailClient,
|
2022-11-21 14:53:05 +02:00
|
|
|
Message: message,
|
2022-10-30 10:28:14 +02:00
|
|
|
Record: authRecord,
|
2022-07-06 23:19:05 +02:00
|
|
|
Meta: map[string]any{"token": token},
|
|
|
|
}
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
sendErr := app.OnMailerBeforeRecordVerificationSend().Trigger(event, func(e *core.MailerRecordEvent) error {
|
2022-11-21 14:53:05 +02:00
|
|
|
return e.MailClient.Send(e.Message)
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if sendErr == nil {
|
2022-10-30 10:28:14 +02:00
|
|
|
app.OnMailerAfterRecordVerificationSend().Trigger(event)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sendErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendUserChangeEmail sends a change email confirmation email to the specified user.
|
2022-10-30 10:28:14 +02:00
|
|
|
func SendRecordChangeEmail(app core.App, record *models.Record, newEmail string) error {
|
|
|
|
token, tokenErr := tokens.NewRecordChangeEmailToken(app, record, newEmail)
|
2022-07-06 23:19:05 +02:00
|
|
|
if tokenErr != nil {
|
|
|
|
return tokenErr
|
|
|
|
}
|
|
|
|
|
|
|
|
mailClient := app.NewMailClient()
|
|
|
|
|
2022-11-26 14:42:45 +02:00
|
|
|
subject, body, err := resolveEmailTemplate(app, token, app.Settings().Meta.ConfirmEmailChangeTemplate)
|
2022-11-21 14:53:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
message := &mailer.Message{
|
|
|
|
From: mail.Address{
|
2022-11-26 14:42:45 +02:00
|
|
|
Name: app.Settings().Meta.SenderName,
|
|
|
|
Address: app.Settings().Meta.SenderAddress,
|
2022-11-21 14:53:05 +02:00
|
|
|
},
|
|
|
|
To: mail.Address{Address: newEmail},
|
|
|
|
Subject: subject,
|
|
|
|
HTML: body,
|
|
|
|
}
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
event := &core.MailerRecordEvent{
|
2022-07-06 23:19:05 +02:00
|
|
|
MailClient: mailClient,
|
2022-11-21 14:53:05 +02:00
|
|
|
Message: message,
|
2022-10-30 10:28:14 +02:00
|
|
|
Record: record,
|
2022-07-06 23:19:05 +02:00
|
|
|
Meta: map[string]any{
|
|
|
|
"token": token,
|
|
|
|
"newEmail": newEmail,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
sendErr := app.OnMailerBeforeRecordChangeEmailSend().Trigger(event, func(e *core.MailerRecordEvent) error {
|
2022-11-21 14:53:05 +02:00
|
|
|
return e.MailClient.Send(e.Message)
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if sendErr == nil {
|
2022-10-30 10:28:14 +02:00
|
|
|
app.OnMailerAfterRecordChangeEmailSend().Trigger(event)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sendErr
|
|
|
|
}
|
2022-08-14 18:30:45 +02:00
|
|
|
|
|
|
|
func resolveEmailTemplate(
|
|
|
|
app core.App,
|
|
|
|
token string,
|
2022-11-26 14:42:45 +02:00
|
|
|
emailTemplate settings.EmailTemplate,
|
2022-08-14 18:30:45 +02:00
|
|
|
) (subject string, body string, err error) {
|
|
|
|
subject, rawBody, _ := emailTemplate.Resolve(
|
2022-11-26 14:42:45 +02:00
|
|
|
app.Settings().Meta.AppName,
|
|
|
|
app.Settings().Meta.AppUrl,
|
2022-08-14 18:30:45 +02:00
|
|
|
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
|
|
|
|
}
|