class MailHandler

Same name in this branch
  1. main core/modules/contact/src/MailHandler.php \Drupal\contact\MailHandler
Same name and namespace in other branches
  1. 11.x core/modules/contact/src/MailHandler.php \Drupal\contact\MailHandler
  2. 10 core/modules/contact/src/MailHandler.php \Drupal\contact\MailHandler
  3. 9 core/modules/contact/src/MailHandler.php \Drupal\contact\MailHandler
  4. 8.9.x core/modules/contact/src/MailHandler.php \Drupal\contact\MailHandler
  5. 11.x core/modules/update/src/MailHandler.php \Drupal\update\MailHandler

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

@internal

Hierarchy

Expanded class hierarchy of MailHandler

1 file declares its use of MailHandler
UpdateCronHooks.php in core/modules/update/src/Hook/UpdateCronHooks.php

File

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

Namespace

Drupal\update
View source
class MailHandler {
  public function __construct(protected readonly EntityTypeManagerInterface $entityTypeManager, protected readonly LanguageManagerInterface $languageManager, protected readonly TimeInterface $time, #[AutowireServiceClosure('plugin.manager.mail')] protected readonly \Closure $mailManager) {
  }
  
  /**
   * Sends update notification mails to a list of recipients.
   *
   * @param string[] $recipients
   *   A list of recipient mail addresses.
   * @param array{'core'?: int, 'contrib'?: int} $items
   *   The update status entries for core and contrib projects. Allowed values
   *   are defined as constants on UpdateManagerInterface.
   *
   * @return bool
   *   TRUE if any email notifications were sent, otherwise FALSE.
   *
   * @see \Drupal\update\UpdateManagerInterface
   */
  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;
  }

}

Members

Title Sort descending Modifiers Object type Summary
MailHandler::sendUpdateNotifications public function Sends update notification mails to a list of recipients.
MailHandler::__construct public function

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