function MailHandler::sendUpdateNotifications

Same name and namespace in other branches
  1. 11.x core/modules/update/src/MailHandler.php \Drupal\update\MailHandler::sendUpdateNotifications()

Sends update notification mails to a list of recipients.

Parameters

string[] $recipients: A list of recipient mail addresses.

array{'core'?: int, 'contrib'?: int} $items: The update status entries for core and contrib projects. Allowed values are defined as constants on UpdateManagerInterface.

Return value

bool TRUE if any email notifications were sent, otherwise FALSE.

See also

\Drupal\update\UpdateManagerInterface

File

core/modules/update/src/MailHandler.php, line 46

Class

MailHandler
A service to handle assembly and dispatch of Update Status mail messages.

Namespace

Drupal\update

Code

public function sendUpdateNotifications(array $recipients, array $items) : bool {
  $results = [];
  $default_langcode = $this->languageManager
    ->getDefaultLanguage()
    ->getId();
  $storage = $this->entityTypeManager
    ->getStorage('user');
  $accounts_by_email = [];
  foreach ($storage->loadByProperties([
    'mail' => $recipients,
  ]) as $account) {
    assert($account instanceof AccountInterface);
    $accounts_by_email[$account->getEmail()] = $account;
  }
  foreach ($recipients as $recipient) {
    // If the recipient is a registered user with a language preference, use
    // the recipient's preferred language. Otherwise, use the system default
    // language.
    $recipient_account = $accounts_by_email[$recipient] ?? NULL;
    $langcode = $recipient_account ? $recipient_account->getPreferredLangcode() : $default_langcode;
    $message = ($this->mailManager)()
      ->mail('update', 'status_notify', $recipient, $langcode, $items);
    $results[] = $message['result'] ?? NULL;
  }
  return count(array_filter($results)) > 0;
}

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