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)
| 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). |
| 54 | func 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 | } |
searching dependent graphs…