You are here

public function MassContact::sendMessage in Mass Contact 8

Sends a message to a list of recipient user IDs.

Parameters

int[] $recipients: An array of recipient user IDs.

\Drupal\mass_contact\Entity\MassContactMessageInterface $message: The mass contact message entity.

array $configuration: An array of configuration. Default values are provided by the mass contact settings.

Overrides MassContactInterface::sendMessage

File

src/MassContact.php, line 259

Class

MassContact
The Mass Contact helper service.

Namespace

Drupal\mass_contact

Code

public function sendMessage(array $recipients, MassContactMessageInterface $message, array $configuration = []) {
  $params = [
    'subject' => $message
      ->getSubject(),
    'body' => $message
      ->getBody(),
    'format' => $message
      ->getFormat(),
    'configuration' => $configuration,
    'headers' => [],
  ];

  // If utilizing BCC, one email is sent.
  if ($configuration['use_bcc']) {
    $emails = [];
    foreach ($recipients as $recipient) {

      /** @var \Drupal\user\UserInterface $account */
      $account = $this->entityTypeManager
        ->getStorage('user')
        ->load($recipient);
      $emails[] = $account
        ->getEmail();
    }
    $params['headers']['Bcc'] = implode(',', array_unique($emails));
    $this->mail
      ->mail('mass_contact', 'mass_contact', $configuration['sender_mail'], \Drupal::languageManager()
      ->getDefaultLanguage()
      ->getId(), $params);
  }
  else {
    foreach ($recipients as $recipient) {

      /** @var \Drupal\user\UserInterface $account */
      $account = $this->entityTypeManager
        ->getStorage('user')
        ->load($recipient);
      $this->mail
        ->mail('mass_contact', 'mass_contact', $account
        ->getEmail(), $account
        ->getPreferredLangcode(), $params);
    }
  }
}