1

I want to send the "admin (user awaiting approval)" email after creating an account in the user_insert hook

I don't know what function is used. I did find _user_mail_notify which works well but only sends emails to registered users (and not to admins). Moreover, we see that _user_mail_notify has 8 possible parameters which correspond to messages sent to users and not to admins (in the UI, there are 9 possibles messages).

I'm looking for a function like _admin_mail_notify or _administrator_mail_notify

I know _admin_mail_notify doesn't exist with that name but maybe that helps to be clearer.

Here is somethings that works to send a user an email :

function my_module_user_insert(\Drupal\user\UserInterface $user) {
    // send email to new user
    _user_mail_notify('register_pending_approval', $user);
    // want to send user awaiting approval email, pseudo code
    // _admin_mail_notify('lorem', $lorem);
}

EDIT 1

I had not specified it to simplify my question but I did not activate the option "Visitors, but administrator approval is required" because I have 2 types of registrants.

Users with an “A” role can register without email verification or admin approval. And another “B” role that must be approved by the admins. By default users can all register and in the hook_user_insert I block the user with $user->block(); so I wanted to also send an email to the admins.

I found this code in user.module which seems to be part of the solution:

if ($op == 'register_pending_approval') {
      // If a user registered requiring admin approval, notify the admin, too.
      // We use the site default language for this.
      \Drupal::service('plugin.manager.mail')->mail('user', 'register_pending_approval_admin', $site_mail, \Drupal::languageManager()->getDefaultLanguage()->getId(), $params);
    }
3
  • Have you enabled admin approval required? That will trigger email to admin.
    – No Sssweat
    Jun 29 at 11:08
  • @NoSssweat I've edited my question. Jun 29 at 11:32
  • 1
    Ok, then just add \Drupal::service('plugin.manager.mail')->mail('user', 'register_pending_approval_admin', $site_mail, \Drupal::languageManager()->getDefaultLanguage()->getId(), $params); into your hook_user_insert code.
    – No Sssweat
    Jun 29 at 11:48

1 Answer 1

1

After inspecting the code in /core/modules/user, I found these lines of code in /core/modules/user/user.module :

if ($op == 'register_pending_approval') {
      // If a user registered requiring admin approval, notify the admin, too.
      // We use the site default language for this.
      \Drupal::service('plugin.manager.mail')->mail('user', 'register_pending_approval_admin', $site_mail, \Drupal::languageManager()->getDefaultLanguage()->getId(), $params);
}

So, I just need to add this in hook_user_insert

function my_module_user_insert(\Drupal\user\UserInterface $user) {
    $params['account'] = $user;
    $site_mail = \Drupal::config('system.site')->get('mail_notification');
    // If the custom site notification email has not been set, we use the site
    // default for this.
    if (empty($site_mail)) {
        $site_mail = \Drupal::config('system.site')->get('mail');
    }
    // send email
    \Drupal::service('plugin.manager.mail')->mail('user', 'register_pending_approval_admin', $site_mail, \Drupal::languageManager()->getDefaultLanguage()->getId(), $params);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.