View source
<?php
namespace Drupal\simplenews\Mail;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Utility\Token;
use Drupal\simplenews\Mail\MailInterface;
use Drupal\simplenews\Subscription\SubscriptionManagerInterface;
class MailBuilder implements MailBuilderInterface {
protected $token;
protected $config;
protected $subscriptionManager;
public function __construct(Token $token, ConfigFactoryInterface $config_factory, SubscriptionManagerInterface $subscription_manager) {
$this->token = $token;
$this->config = $config_factory
->get('simplenews.settings');
$this->subscriptionManager = $subscription_manager;
}
function buildNewsletterMail(array &$message, MailInterface $mail) {
$message['headers'] = $mail
->getHeaders($message['headers']);
$message['subject'] = $mail
->getSubject();
$message['body']['body'] = $mail
->getBody();
if ($mail
->getFormat() == 'html') {
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8';
$message['params']['format'] = 'text/html';
$message['params']['plain'] = NULL;
$message['params']['plaintext'] = $mail
->getPlainBody();
$message['plain'] = $message['params']['plaintext'];
$message['params']['attachments'] = $mail
->getAttachments();
$message['params']['files'] = $message['params']['attachments'];
}
else {
$message['params']['plain'] = TRUE;
$message['params']['format'] = 'text/plain';
}
}
function buildSubscribeMail(array &$message, array $params) {
$context = $params['context'];
$message['headers']['From'] = $params['from']['formatted'];
$message['subject'] = $this->config
->get('subscription.confirm_subscribe_subject');
$message['subject'] = $this->token
->replace($message['subject'], $context, array(
'sanitize' => FALSE,
));
if ($context['simplenews_subscriber']
->isSubscribed($context['newsletter']
->id())) {
$body = $this->config
->get('subscription.confirm_subscribe_subscribed');
}
else {
$body = $this->config
->get('subscription.confirm_subscribe_unsubscribed');
}
$message['body'][] = $this->token
->replace($body, $context, array(
'sanitize' => FALSE,
));
}
function buildCombinedMail(&$message, $params) {
$context = $params['context'];
$subscriber = $context['simplenews_subscriber'];
$langcode = $message['langcode'];
$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";
$subscribed = $context['simplenews_subscriber']
->isSubscribed($newsletter_id);
$changes = $subscriber
->getChanges();
if ($changes[$newsletter_id] == 'subscribe' && !$subscribed || $changes[$newsletter_id] == 'unsubscribe' && $subscribed) {
$actual_changes++;
}
}
$body_key = $actual_changes ? 'combined_body' : 'combined_body_unchanged';
$body = $this->config
->get('subscription.confirm_' . $body_key);
$body = str_replace('[changes-list]', $changes_list, $body);
$message['body'][] = $this->token
->replace($body, $context, array(
'sanitize' => FALSE,
));
}
function buildUnsubscribeMail(&$message, $params) {
$context = $params['context'];
$message['headers']['From'] = $params['from']['formatted'];
$message['subject'] = $this->config
->get('subscription.confirm_subscribe_subject');
$message['subject'] = $this->token
->replace($message['subject'], $context, array(
'sanitize' => FALSE,
));
if ($context['simplenews_subscriber']
->isSubscribed($context['newsletter']
->id())) {
$body = $this->config
->get('subscription.confirm_unsubscribe_subscribed');
$message['body'][] = $this->token
->replace($body, $context, array(
'sanitize' => FALSE,
));
}
else {
$body = $this->config
->get('subscription.confirm_unsubscribe_unsubscribed');
$message['body'][] = $this->token
->replace($body, $context, array(
'sanitize' => FALSE,
));
}
}
}