function _user_mail_notify

Same name and namespace in other branches
  1. 11.x core/modules/user/user.module \_user_mail_notify()
  2. 10 core/modules/user/user.module \_user_mail_notify()
  3. 9 core/modules/user/user.module \_user_mail_notify()
  4. 8.9.x core/modules/user/user.module \_user_mail_notify()
  5. 7.x modules/user/user.module \_user_mail_notify()

Creates and sends a notification email following a change to a user account.

Parameters

string $op: The operation being performed on the account. Possible values:

  • 'register_admin_created': Welcome message for user created by the admin.
  • 'register_no_approval_required': Welcome message when user self-registers.
  • 'register_pending_approval': Welcome message, user pending admin approval.
  • 'password_reset': Password recovery request.
  • 'status_activated': Account activated.
  • 'status_blocked': Account blocked.
  • 'cancel_confirm': Account cancellation request.
  • 'status_canceled': Account canceled.

\Drupal\Core\Session\AccountInterface $account: The user object of the account being notified. Must contain at least the fields 'uid', 'name', and 'mail'.

Return value

array|null An array containing various information about the message. See \Drupal\Core\Mail\MailManagerInterface::mail() for details. Or null if the account does not have an email address.

See also

user_mail_tokens()

10 calls to _user_mail_notify()
RegisterForm::save in core/modules/user/src/RegisterForm.php
User::postSave in core/modules/user/src/Entity/User.php
UserAuthenticationController::resetPassword in core/modules/user/src/Controller/UserAuthenticationController.php
Resets a user password.
UserCancelForm::submitForm in core/modules/user/src/Form/UserCancelForm.php
UserMailNotifyTest::testUserMailsNotSent in core/modules/user/tests/src/Kernel/UserMailNotifyTest.php
Tests mails are not sent when notify.$op is FALSE.

... See full list

File

core/modules/user/user.module, line 525

Code

function _user_mail_notify($op, AccountInterface $account) {
  if (\Drupal::config('user.settings')->get('notify.' . $op)) {
    $params['account'] = $account;
    // Get the custom site notification email to use as the from email address
    // if it has been set.
    $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');
    }
    if ($account->getEmail()) {
      $mail = \Drupal::service('plugin.manager.mail')->mail('user', $op, $account->getEmail(), $account->getPreferredLangcode(), $params, $site_mail);
    }
    else {
      \Drupal::logger('user')->info('The User module could not send an email for the operation "%op" because the user account %account does not have an email address.', [
        '%op' => $op,
        '%account' => $account->getDisplayName(),
      ]);
    }
    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);
    }
  }
  return empty($mail) ? NULL : $mail['result'];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.