You are here

protected function EmailHandler::createUnsafeEmailsForRecipients in Easy Email 8

Same name and namespace in other branches
  1. 2.0.x src/Service/EmailHandler.php \Drupal\easy_email\Service\EmailHandler::createUnsafeEmailsForRecipients()

Parameters

\Drupal\easy_email\Entity\EasyEmailInterface $email:

array $params:

Return value

array

1 call to EmailHandler::createUnsafeEmailsForRecipients()
EmailHandler::sendEmail in src/Service/EmailHandler.php
@inheritDoc

File

src/Service/EmailHandler.php, line 220

Class

EmailHandler

Namespace

Drupal\easy_email\Service

Code

protected function createUnsafeEmailsForRecipients(EasyEmailInterface $email, array $params) {
  $emails = [];
  $recipients = $email
    ->getRecipients();
  foreach ($recipients as $recipient) {
    if (empty($emails)) {

      // The first email should use the original email object
      $user_email = $email;
    }
    else {
      $user_email = $email
        ->createDuplicate();
    }
    $user_email
      ->setRecipientIds([
      $recipient
        ->id(),
    ]);
    $user_email
      ->setRecipientAddresses([
      $recipient
        ->getEmail(),
    ]);
    $user_email
      ->setCCIds(NULL);
    $user_email
      ->setCCAddresses(NULL);
    $user_email
      ->setBCCIds(NULL);
    $user_email
      ->setBCCAddresses(NULL);
    $user_params = $params;
    if (isset($user_params['headers']['Cc'])) {
      unset($user_params['headers']['Cc']);
    }
    if (isset($user_params['headers']['Bcc'])) {
      unset($user_params['headers']['Bcc']);
    }
    $unsafe_user_email = $user_email
      ->createDuplicate();
    $html_body = $user_email
      ->getHtmlBody();
    if (!empty($html_body)) {
      $unsafe_user_email
        ->setHtmlBody($this->tokenEvaluator
        ->replaceUnsafeTokens($html_body['value'], $recipient), $html_body['format']);
      $user_params['body'] = $this
        ->buildHtmlBody($unsafe_user_email);
    }
    $plain_body = $user_email
      ->getPlainBody();
    if (!empty($plain_body)) {
      $unsafe_user_email
        ->setPlainBody($this->tokenEvaluator
        ->replaceUnsafeTokens($plain_body, $recipient));
      $user_params['plain'] = $this
        ->buildPlainBody($unsafe_user_email);
    }
    $emails[] = [
      'to' => $recipient
        ->getEmail(),
      'email' => $user_email,
      'params' => $user_params,
    ];
  }
  return $emails;
}