1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-03-19 22:19:23 +02:00
pocketbase/mails/record.go

241 lines
6.4 KiB
Go
Raw Normal View History

2022-07-07 00:19:05 +03:00
package mails
import (
"html/template"
2022-07-07 00:19:05 +03:00
"net/mail"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/mails/templates"
"github.com/pocketbase/pocketbase/tools/mailer"
2022-07-07 00:19:05 +03:00
)
2024-09-29 19:23:19 +03:00
// SendRecordAuthAlert sends a new device login alert to the specified auth record.
func SendRecordAuthAlert(app core.App, authRecord *core.Record) error {
mailClient := app.NewMailClient()
subject, body, err := resolveEmailTemplate(app, authRecord, authRecord.Collection().AuthAlert.EmailTemplate, nil)
if err != nil {
return err
}
2024-09-29 19:23:19 +03:00
message := &mailer.Message{
From: mail.Address{
Name: app.Settings().Meta.SenderName,
Address: app.Settings().Meta.SenderAddress,
},
To: []mail.Address{{Address: authRecord.Email()}},
Subject: subject,
HTML: body,
}
event := new(core.MailerRecordEvent)
event.App = app
event.Mailer = mailClient
event.Message = message
event.Record = authRecord
return app.OnMailerRecordAuthAlertSend().Trigger(event, func(e *core.MailerRecordEvent) error {
return e.Mailer.Send(e.Message)
})
}
// SendRecordOTP sends OTP email to the specified auth record.
func SendRecordOTP(app core.App, authRecord *core.Record, otpId string, pass string) error {
mailClient := app.NewMailClient()
2024-09-29 19:23:19 +03:00
subject, body, err := resolveEmailTemplate(app, authRecord, authRecord.Collection().OTP.EmailTemplate, map[string]any{
core.EmailPlaceholderOTPId: otpId,
core.EmailPlaceholderOTP: pass,
})
if err != nil {
return err
}
message := &mailer.Message{
From: mail.Address{
Name: app.Settings().Meta.SenderName,
Address: app.Settings().Meta.SenderAddress,
},
To: []mail.Address{{Address: authRecord.Email()}},
2024-09-29 19:23:19 +03:00
Subject: subject,
HTML: body,
}
2024-09-29 19:23:19 +03:00
event := new(core.MailerRecordEvent)
event.App = app
event.Mailer = mailClient
event.Message = message
event.Record = authRecord
event.Meta = map[string]any{
"otpId": otpId,
"password": pass,
}
return app.OnMailerRecordOTPSend().Trigger(event, func(e *core.MailerRecordEvent) error {
return e.Mailer.Send(e.Message)
})
}
2024-09-29 19:23:19 +03:00
// SendRecordPasswordReset sends a password reset request email to the specified auth record.
func SendRecordPasswordReset(app core.App, authRecord *core.Record) error {
token, tokenErr := authRecord.NewPasswordResetToken()
2022-07-07 00:19:05 +03:00
if tokenErr != nil {
return tokenErr
}
mailClient := app.NewMailClient()
2024-09-29 19:23:19 +03:00
subject, body, err := resolveEmailTemplate(app, authRecord, authRecord.Collection().ResetPasswordTemplate, map[string]any{
core.EmailPlaceholderToken: token,
})
if err != nil {
return err
}
message := &mailer.Message{
From: mail.Address{
Name: app.Settings().Meta.SenderName,
Address: app.Settings().Meta.SenderAddress,
},
To: []mail.Address{{Address: authRecord.Email()}},
Subject: subject,
HTML: body,
}
event := new(core.MailerRecordEvent)
2024-09-29 19:23:19 +03:00
event.App = app
event.Mailer = mailClient
event.Message = message
event.Record = authRecord
event.Meta = map[string]any{"token": token}
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
return app.OnMailerRecordPasswordResetSend().Trigger(event, func(e *core.MailerRecordEvent) error {
return e.Mailer.Send(e.Message)
2023-07-20 10:40:03 +03:00
})
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
// SendRecordVerification sends a verification request email to the specified auth record.
func SendRecordVerification(app core.App, authRecord *core.Record) error {
token, tokenErr := authRecord.NewVerificationToken()
2022-07-07 00:19:05 +03:00
if tokenErr != nil {
return tokenErr
}
mailClient := app.NewMailClient()
2024-09-29 19:23:19 +03:00
subject, body, err := resolveEmailTemplate(app, authRecord, authRecord.Collection().VerificationTemplate, map[string]any{
core.EmailPlaceholderToken: token,
})
if err != nil {
return err
}
message := &mailer.Message{
From: mail.Address{
Name: app.Settings().Meta.SenderName,
Address: app.Settings().Meta.SenderAddress,
},
To: []mail.Address{{Address: authRecord.Email()}},
Subject: subject,
HTML: body,
}
event := new(core.MailerRecordEvent)
2024-09-29 19:23:19 +03:00
event.App = app
event.Mailer = mailClient
event.Message = message
event.Record = authRecord
event.Meta = map[string]any{"token": token}
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
return app.OnMailerRecordVerificationSend().Trigger(event, func(e *core.MailerRecordEvent) error {
return e.Mailer.Send(e.Message)
2023-07-20 10:40:03 +03:00
})
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
// SendRecordChangeEmail sends a change email confirmation email to the specified auth record.
func SendRecordChangeEmail(app core.App, authRecord *core.Record, newEmail string) error {
token, tokenErr := authRecord.NewEmailChangeToken(newEmail)
2022-07-07 00:19:05 +03:00
if tokenErr != nil {
return tokenErr
}
mailClient := app.NewMailClient()
2024-09-29 19:23:19 +03:00
subject, body, err := resolveEmailTemplate(app, authRecord, authRecord.Collection().ConfirmEmailChangeTemplate, map[string]any{
core.EmailPlaceholderToken: token,
})
if err != nil {
return err
}
message := &mailer.Message{
From: mail.Address{
Name: app.Settings().Meta.SenderName,
Address: app.Settings().Meta.SenderAddress,
},
To: []mail.Address{{Address: newEmail}},
Subject: subject,
HTML: body,
}
event := new(core.MailerRecordEvent)
2024-09-29 19:23:19 +03:00
event.App = app
event.Mailer = mailClient
event.Message = message
2024-09-29 19:23:19 +03:00
event.Record = authRecord
event.Meta = map[string]any{
"token": token,
"newEmail": newEmail,
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
return app.OnMailerRecordEmailChangeSend().Trigger(event, func(e *core.MailerRecordEvent) error {
return e.Mailer.Send(e.Message)
2023-07-20 10:40:03 +03:00
})
2022-07-07 00:19:05 +03:00
}
func resolveEmailTemplate(
app core.App,
2024-09-29 19:23:19 +03:00
authRecord *core.Record,
emailTemplate core.EmailTemplate,
placeholders map[string]any,
) (subject string, body string, err error) {
2024-09-29 19:23:19 +03:00
if placeholders == nil {
placeholders = map[string]any{}
}
// register default system placeholders
if _, ok := placeholders[core.EmailPlaceholderAppName]; !ok {
placeholders[core.EmailPlaceholderAppName] = app.Settings().Meta.AppName
}
if _, ok := placeholders[core.EmailPlaceholderAppURL]; !ok {
placeholders[core.EmailPlaceholderAppURL] = app.Settings().Meta.AppURL
}
// register default auth record placeholders
for _, field := range authRecord.Collection().Fields {
if field.GetHidden() {
continue
}
fieldPlacehodler := "{RECORD:" + field.GetName() + "}"
if _, ok := placeholders[fieldPlacehodler]; !ok {
placeholders[fieldPlacehodler] = authRecord.Get(field.GetName())
}
}
subject, rawBody := emailTemplate.Resolve(placeholders)
params := struct {
2024-09-29 19:23:19 +03:00
HTMLContent template.HTML
}{
2024-09-29 19:23:19 +03:00
HTMLContent: template.HTML(rawBody),
}
2024-09-29 19:23:19 +03:00
body, err = resolveTemplateContent(params, templates.Layout, templates.HTMLBody)
if err != nil {
return "", "", err
}
return subject, body, nil
}