Hook: on_user_change_email
avatar
Jérôme Kägi


Hook: on_user_change_email

The on_user_change_email hook is executed whenever a user's email address is updated. This hook allows plugins to perform additional actions or validations when an email address change occurs.

Usage


DynamicHooks::executeHook('on_user_change_email', false, $oldEmail, $newEmail);
    

This hook is triggered with the following parameters:

  • $oldEmail: (string) The user's current/previous email address before the change.
  • $newEmail: (string) The user's updated/new email address.

Purpose

The on_user_change_email hook is primarily used for:

  • Updating email addresses in external systems integrated via plugins.
  • Sending confirmation or notification emails to the new email address.
  • Validating the new email address for specific requirements (e.g., domain restrictions).
  • Logging email address changes for audit purposes.

Example Implementation


DynamicHooks::addHook('on_user_change_email', function ($oldEmail, $newEmail) use ($jakdb, $lang) {
    // Log the email change
    $jakdb->insert('email_change_log', [
        'old_email' => $oldEmail,
        'new_email' => $newEmail,
        'changed_at' => date('Y-m-d H:i:s'),
    ]);

    // Send a notification to the new email
    JScms_send_email($jakdb, $newEmail, $mailccaddress, $replyto, $subject, $message, $emailfiles);
}, 'email_change_plugin');
    

Best Practices

  • Ensure email addresses are properly validated before executing actions within this hook.
  • Test the hook with both valid and invalid email changes to prevent unexpected behavior.
  • Log the changes to a database or file for security and auditing purposes.

Common Issues

  • Validation Errors: Ensure both the old and new email addresses are properly formatted and validated.
  • Notification Failures: Test email sending functionality to ensure notifications are reliably delivered.
  • Performance: Avoid running resource-intensive tasks within the hook to ensure smooth user experience.
Was this article helpful?
0 out of 0 found this helpful