You are here

function _user_mail_notify in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/user.module \_user_mail_notify()
  2. 6 modules/user/user.module \_user_mail_notify()
  3. 7 modules/user/user.module \_user_mail_notify()

Conditionally create and send a notification email when a certain operation happens on the given 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'.

string $langcode: (deprecated) (optional) Language code to use for the notification, overriding account language. Specifying the notification language using the $langcode parameter is deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Omit the parameter. See https://www.drupal.org/node/3187082

Return value

array An array containing various information about the message. See \Drupal\Core\Mail\MailManagerInterface::mail() for details.

See also

user_mail_tokens()

10 calls to _user_mail_notify()
RegisterForm::save in core/modules/user/src/RegisterForm.php
Form submission handler for the 'save' action.
User::postSave in core/modules/user/src/Entity/User.php
Acts on a saved entity before the insert or update hook is invoked.
UserAuthenticationController::resetPassword in core/modules/user/src/Controller/UserAuthenticationController.php
Resets a user password.
UserCancelForm::submitForm in core/modules/user/src/Form/UserCancelForm.php
This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state…
UserMailNotifyTest::testUserMailNotifyLangcodeDeprecation in core/modules/user/tests/src/Kernel/UserMailNotifyTest.php
Tests the deprecated $langcode argument to _user_mail_notify().

... See full list

File

core/modules/user/user.module, line 1045
Enables the user registration and login system.

Code

function _user_mail_notify($op, AccountInterface $account, $langcode = NULL) {
  if ($langcode) {
    @trigger_error('Specifying the notification language using the $langcode parameter is deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Omit the parameter. See https://www.drupal.org/node/3187082', E_USER_DEPRECATED);
  }
  if (\Drupal::config('user.settings')
    ->get('notify.' . $op)) {
    $params['account'] = $account;
    $langcode = $langcode ? $langcode : $account
      ->getPreferredLangcode();

    // 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 (empty($site_mail)) {
      $site_mail = ini_get('sendmail_from');
    }
    $mail = \Drupal::service('plugin.manager.mail')
      ->mail('user', $op, $account
      ->getEmail(), $langcode, $params, $site_mail);
    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'];
}