MCPcopy
hub / github.com/pocketbase/pocketbase / SendRecordOTP

Function SendRecordOTP

mails/record.go:54–125  ·  view source on GitHub ↗

SendRecordOTP sends OTP email to the specified auth record. This method will also update the "sentTo" field of the related OTP record to the mail sent To address (if the OTP exists and not already assigned).

(app core.App, authRecord *core.Record, otpId string, pass string)

Source from the content-addressed store, hash-verified

52//
53// This method will also update the "sentTo" field of the related OTP record to the mail sent To address (if the OTP exists and not already assigned).
54func SendRecordOTP(app core.App, authRecord *core.Record, otpId string, pass string) error {
55 mailClient := app.NewMailClient()
56
57 subject, body, err := resolveEmailTemplate(app, authRecord, authRecord.Collection().OTP.EmailTemplate, map[string]any{
58 core.EmailPlaceholderOTPId: otpId,
59 core.EmailPlaceholderOTP: pass,
60 })
61 if err != nil {
62 return err
63 }
64
65 message := &mailer.Message{
66 From: mail.Address{
67 Name: app.Settings().Meta.SenderName,
68 Address: app.Settings().Meta.SenderAddress,
69 },
70 To: []mail.Address{{Address: authRecord.Email()}},
71 Subject: subject,
72 HTML: body,
73 }
74
75 event := new(core.MailerRecordEvent)
76 event.App = app
77 event.Mailer = mailClient
78 event.Message = message
79 event.Record = authRecord
80 event.Meta = map[string]any{
81 "otpId": otpId,
82 "password": pass,
83 }
84
85 return app.OnMailerRecordOTPSend().Trigger(event, func(e *core.MailerRecordEvent) error {
86 err := e.Mailer.Send(e.Message)
87 if err != nil {
88 return err
89 }
90
91 var toAddress string
92 if len(e.Message.To) > 0 {
93 toAddress = e.Message.To[0].Address
94 }
95 if toAddress == "" {
96 return nil
97 }
98
99 otp, err := e.App.FindOTPById(otpId)
100 if err != nil {
101 e.App.Logger().Warn(
102 "Unable to find OTP to update its sentTo field (either it was already deleted or the id is nonexisting)",
103 "error", err,
104 "otpId", otpId,
105 )
106 return nil
107 }
108
109 if otp.SentTo() != "" {
110 return nil // was already sent to another target
111 }

Callers 3

TestSendRecordOTPFunction · 0.92
recordRequestOTPFunction · 0.92
SubmitMethod · 0.92

Calls 14

resolveEmailTemplateFunction · 0.85
CollectionMethod · 0.80
EmailMethod · 0.80
TriggerMethod · 0.80
SentToMethod · 0.80
SetSentToMethod · 0.80
NewMailClientMethod · 0.65
SettingsMethod · 0.65
OnMailerRecordOTPSendMethod · 0.65
SendMethod · 0.65
FindOTPByIdMethod · 0.65
LoggerMethod · 0.65

Tested by 1

TestSendRecordOTPFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…