password mail user
This commit is contained in:
parent
156d96adfd
commit
2f1f559383
111
main.go
111
main.go
@ -111,6 +111,117 @@ func main() {
|
||||
// app := pocketbase.New()
|
||||
app = pocketbase.New()
|
||||
|
||||
app.OnRecordBeforeUpdateRequest().Add(func(e *core.RecordUpdateEvent) error {
|
||||
if e.Collection.Name == "users" {
|
||||
// Fetch the original record by ID
|
||||
originalRecord, err := app.Dao().FindRecordById("users", e.Record.GetString("id"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch original record: %v", err)
|
||||
}
|
||||
|
||||
// Compare the original `passwordHash` with the updated `passwordHash`
|
||||
originalPasswordHash := originalRecord.GetString("passwordHash")
|
||||
updatedPasswordHash := e.Record.GetString("passwordHash")
|
||||
|
||||
if originalPasswordHash != updatedPasswordHash {
|
||||
fmt.Println("Password changed for user:", e.Record.GetString("username"))
|
||||
|
||||
// Fetch user details for the email
|
||||
name := e.Record.GetString("username")
|
||||
email := e.Record.GetString("email")
|
||||
|
||||
if name == "" || email == "" {
|
||||
return fmt.Errorf("user details missing: name or email")
|
||||
}
|
||||
|
||||
// Send an email notification
|
||||
subject := "Password Change Notification"
|
||||
body := fmt.Sprintf(`
|
||||
<p>Hello %s,</p>
|
||||
<p>Your password has been successfully changed. If this was not you, please contact support immediately.</p>
|
||||
<p>Best regards,<br>Your Team</p>
|
||||
`, name)
|
||||
|
||||
// Create the email message
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Name: "FCSC", // Sender name
|
||||
Address: app.Settings().Meta.SenderAddress, // Sender email address from app settings
|
||||
},
|
||||
To: []mail.Address{
|
||||
{
|
||||
Name: name,
|
||||
Address: email,
|
||||
},
|
||||
},
|
||||
Subject: subject,
|
||||
HTML: body, // Use HTML body for formatted content
|
||||
}
|
||||
|
||||
// Send the email
|
||||
err := app.NewMailClient().Send(message)
|
||||
if err != nil {
|
||||
log.Printf("Failed to send password change email to user: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
// Handle password resets explicitly
|
||||
|
||||
app.OnRecordAfterAuthWithPasswordRequest().Add(func(e *core.RecordAuthWithPasswordEvent) error {
|
||||
if e.Collection.Name == "users" {
|
||||
fmt.Println("Password reset detected for user:", e.Record.GetString("username"))
|
||||
|
||||
// Optionally, log the password reset time or other actions
|
||||
resetTime := time.Now().Format(time.RFC3339)
|
||||
fmt.Println("Password reset time:", resetTime)
|
||||
|
||||
// Fetch user details for the email
|
||||
name := e.Record.GetString("username")
|
||||
email := e.Record.GetString("email")
|
||||
|
||||
if name == "" || email == "" {
|
||||
return fmt.Errorf("user details missing: name or email")
|
||||
}
|
||||
|
||||
// Send an email notification
|
||||
subject := "Password Reset Notification"
|
||||
body := fmt.Sprintf(`
|
||||
<p>Hello %s,</p>
|
||||
<p>Your password has been successfully reset on %s. If this was not you, please contact support immediately.</p>
|
||||
<p>Best regards,<br>Your Team</p>
|
||||
`, name, resetTime)
|
||||
|
||||
// Create the email message
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Name: "FCSC", // Sender name
|
||||
Address: app.Settings().Meta.SenderAddress, // Sender email address from app settings
|
||||
},
|
||||
To: []mail.Address{
|
||||
{
|
||||
Name: name,
|
||||
Address: email,
|
||||
},
|
||||
},
|
||||
Subject: subject,
|
||||
HTML: body, // Use HTML body for formatted content
|
||||
}
|
||||
|
||||
// Send the email
|
||||
if err := app.NewMailClient().Send(message); err != nil {
|
||||
log.Printf("Failed to send password reset email to user: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
//send mail while status changed from app admin
|
||||
app.OnRecordBeforeUpdateRequest().Add(func(e *core.RecordUpdateEvent) error {
|
||||
if e.Collection.Name == "users" {
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user