function NotificationHandler::sendToUser

Same name and namespace in other branches
  1. 11.x core/modules/user/src/NotificationHandler.php \Drupal\user\NotificationHandler::sendToUser()

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\user\UserInterface $account: The entity that represents the user who is being notified.

Return value

bool|null TRUE if the email has been sent successfully, FALSE if an error occurred, NULL if the sending has been canceled by an implementation of hook_mail_alter().

See also

user_mail_tokens()

File

core/modules/user/src/NotificationHandler.php, line 219

Class

NotificationHandler
Implements a service to dispatch user notification messages.

Namespace

Drupal\user

Code

protected function sendToUser(string $op, UserInterface $account) : ?bool {
  if ($this->configFactory
    ->get('user.settings')
    ->get('notify.' . $op)) {
    $params['account'] = $account;
    if ($account->getEmail()) {
      $mail = ($this->mailManager)()
        ->mail('user', $op, $account->getEmail(), $account->getPreferredLangcode(), $params, $this->getReplyToAddress());
    }
    else {
      ($this->logger)()
        ->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(),
      ]);
    }
  }
  return empty($mail) ? NULL : $mail['result'];
}

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