User Registration Notification and hook_mail()

Draft status not available -- AWAITING PEER REVIEW
  • Basic knowledge of PHP and CSS required
  • Knowledge of basic module creation

Step 1: Create a new module. Name it register_notify. See the lesson about creating new modules for details about specific steps.

Step 2: Review the API documentation for the hook_user_insert() function at!user!user.api.php/function/hook... . This hook gets called whenever a new user is created. You can take any custom action you want here. We’ll send an email notification Paste the following code into your new module:

* Implements hook_user_insert().
function register_notify_user_insert(&$edit, &$account) {


Step 3: Use hook_mail() to prepare a message to pass to drupal_mail(). hook_mail() is tragically complicated, but no fear. You can tame the beast! For details, see the documentation page at!system!system.api.php/function/... .

* Implements hook_mail().
function register_notify_mail($key, &$message, $params) {
switch ($key) {
case ‘new_user_notify’:
$message[‘subject’] = t(‘A new user was created!’, array());
$message[‘body’][] = t(‘’, array());


Step 4:
Enable the register_notify module. Register a new user at /user/register and verify that the admin receives an email notification.

Lesson tags: