MailBuilder.php in Simplenews 3.x
File
src/Mail/MailBuilder.php
View source
<?php
namespace Drupal\simplenews\Mail;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Utility\Token;
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;
}
public 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;
if ($this->config
->get('mail.textalt')) {
$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';
}
}
public function buildCombinedMail(array &$message, array $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'] = simplenews_token_replace_subject($message['subject'], $context);
$actual_changes = 0;
foreach ($subscriber
->getChanges() as $newsletter_id => $action) {
$subscribed = $context['simplenews_subscriber']
->isSubscribed($newsletter_id);
if ($action == 'subscribe' && !$subscribed || $action == 'unsubscribe' && $subscribed) {
$actual_changes++;
}
}
$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);
}
public function buildValidateMail(array &$message, array $params) {
$context = $params['context'];
$message['headers']['From'] = $params['from']['formatted'];
$message['subject'] = $this->config
->get('subscription.validate_subject');
$message['subject'] = simplenews_token_replace_subject($message['subject'], $context);
$body = $this->config
->get('subscription.validate_body');
$message['body'][] = simplenews_token_replace_body($body, $context);
}
}