You are here

function MailBuilder::buildCombinedMail in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Mail/MailBuilder.php \Drupal\simplenews\Mail\MailBuilder::buildCombinedMail()
  2. 3.x src/Mail/MailBuilder.php \Drupal\simplenews\Mail\MailBuilder::buildCombinedMail()

Build subject and body of the subscribe confirmation email.

Parameters

array $message: Message array as used by hook_mail().

array $params: Parameter array as used by hook_mail().

Overrides MailBuilderInterface::buildCombinedMail

File

src/Mail/MailBuilder.php, line 105

Class

MailBuilder
Default mail builder.

Namespace

Drupal\simplenews\Mail

Code

function buildCombinedMail(&$message, $params) {
  $context = $params['context'];
  $subscriber = $context['simplenews_subscriber'];
  $langcode = $message['langcode'];

  // Use formatted from address "name" <mail_address>
  $message['headers']['From'] = $params['from']['formatted'];
  $message['subject'] = $this->config
    ->get('subscription.confirm_combined_subject');
  $message['subject'] = $this->token
    ->replace($message['subject'], $context, array(
    'sanitize' => FALSE,
  ));
  $changes_list = '';
  $actual_changes = 0;
  foreach ($this->subscriptionManager
    ->getChangesList($context['simplenews_subscriber'], $subscriber
    ->getChanges(), $langcode) as $newsletter_id => $change) {
    $changes_list .= ' - ' . $change . "\n";

    // Count the actual changes.
    $subscribed = $context['simplenews_subscriber']
      ->isSubscribed($newsletter_id);
    $changes = $subscriber
      ->getChanges();
    if ($changes[$newsletter_id] == 'subscribe' && !$subscribed || $changes[$newsletter_id] == 'unsubscribe' && $subscribed) {
      $actual_changes++;
    }
  }

  // If there are actual changes, use the combined_body key otherwise use the
  // one without a confirmation link.
  $body_key = $actual_changes ? 'combined_body' : 'combined_body_unchanged';
  $body = $this->config
    ->get('subscription.confirm_' . $body_key);

  // The changes list is not an actual token.
  $body = str_replace('[changes-list]', $changes_list, $body);
  $message['body'][] = $this->token
    ->replace($body, $context, array(
    'sanitize' => FALSE,
  ));
}