You are here

public function MailBuilder::buildCombinedMail in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 src/Mail/MailBuilder.php \Drupal\simplenews\Mail\MailBuilder::buildCombinedMail()
  2. 8 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 92

Class

MailBuilder
Default mail builder.

Namespace

Drupal\simplenews\Mail

Code

public function buildCombinedMail(array &$message, array $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'] = simplenews_token_replace_subject($message['subject'], $context);
  $actual_changes = 0;
  foreach ($subscriber
    ->getChanges() as $newsletter_id => $action) {

    // Count the actual changes.
    $subscribed = $context['simplenews_subscriber']
      ->isSubscribed($newsletter_id);
    if ($action == 'subscribe' && !$subscribed || $action == '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);
  $message['body'][] = simplenews_token_replace_body($body, $context);
}