View source
<?php
namespace Drupal\mass_contact;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Session\AccountInterface;
use Drupal\mass_contact\Entity\MassContactMessageInterface;
class MassContact implements MassContactInterface {
const MAX_QUEUE_RECIPIENTS = 50;
protected static $htmlEmailModules = [
'mimemail',
'swiftmailer',
];
protected $config;
protected $moduleHandler;
protected $optOut;
protected $processingQueue;
protected $sendingQueue;
protected $mail;
protected $entityTypeManager;
protected $currentUser;
public function __construct(ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, QueueFactory $queue, MailManagerInterface $mail_manager, EntityTypeManagerInterface $entity_type_manager, OptOutInterface $opt_out, AccountInterface $current_user) {
$this->moduleHandler = $module_handler;
$this->config = $config_factory
->get('mass_contact.settings');
$this->optOut = $opt_out;
$this->processingQueue = $queue
->get('mass_contact_queue_messages', TRUE);
$this->sendingQueue = $queue
->get('mass_contact_send_message', TRUE);
$this->mail = $mail_manager;
$this->entityTypeManager = $entity_type_manager;
$this->currentUser = $current_user;
}
public function htmlSupported() {
foreach (static::$htmlEmailModules as $module) {
if ($this->moduleHandler
->moduleExists($module)) {
return TRUE;
}
}
return FALSE;
}
public function processMassContactMessage(MassContactMessageInterface $message, array $configuration = []) {
$configuration += $this
->getDefaultConfiguration();
$data = [
'message' => $message,
'configuration' => $configuration,
];
$this->processingQueue
->createItem($data);
if ($configuration['create_archive_copy']) {
$message
->save();
}
}
protected function getDefaultConfiguration() {
$default = [
'use_bcc' => $this->config
->get('use_bcc'),
'sender_name' => $this->config
->get('default_sender_name'),
'sender_mail' => $this->config
->get('default_sender_email'),
'create_archive_copy' => $this->config
->get('create_archive_copy'),
'send_me_copy_user' => FALSE,
'respect_opt_out' => TRUE,
];
return $default;
}
public function queueRecipients(MassContactMessageInterface $message, array $configuration = []) {
$configuration += $this
->getDefaultConfiguration();
$data = [
'message' => $message,
'configuration' => $configuration,
];
$all_recipients = $this
->getRecipients($message
->getCategories(), $configuration['respect_opt_out']);
$send_me_copy_user = $data['configuration']['send_me_copy_user'];
if ($send_me_copy_user) {
if (!empty($all_recipients)) {
$send_me_copy_user_key = array_search($send_me_copy_user, $all_recipients);
if ($send_me_copy_user_key !== FALSE) {
unset($all_recipients[$send_me_copy_user_key]);
}
}
$all_recipients = [
$send_me_copy_user => $send_me_copy_user,
] + $all_recipients;
}
foreach ($this
->getGroupedRecipients($all_recipients) as $recipients) {
$data['recipients'] = $recipients;
$this->sendingQueue
->createItem($data);
}
}
public function getGroupedRecipients(array $all_recipients) {
$groupings = [];
$recipients = [];
foreach ($all_recipients as $recipient) {
$recipients[] = $recipient;
if (count($recipients) == static::MAX_QUEUE_RECIPIENTS) {
$groupings[] = $recipients;
$recipients = [];
}
}
if (!empty($recipients)) {
$groupings[] = $recipients;
}
return $groupings;
}
public function getRecipients(array $categories, $respect_opt_out) {
$recipients = [];
if (!empty($categories)) {
foreach ($categories as $category) {
$category_recipients = [];
foreach ($category
->getRecipients() as $plugin_id => $config) {
$grouping = $category
->getGroupingCategories($plugin_id);
if (!empty($config['categories'])) {
$category_recipients[$plugin_id] = $grouping
->getRecipients($config['categories']);
}
}
$recipients += count($category_recipients) > 1 ? call_user_func_array('array_intersect', $category_recipients) : reset($category_recipients);
}
if ($respect_opt_out) {
return array_diff_key($recipients, $this->optOut
->getOptOutAccounts($categories));
}
}
return $recipients;
}
public function sendMessage(array $recipients, MassContactMessageInterface $message, array $configuration = []) {
$params = [
'subject' => $message
->getSubject(),
'body' => $message
->getBody(),
'format' => $message
->getFormat(),
'configuration' => $configuration,
'headers' => [],
];
if ($configuration['use_bcc']) {
$emails = [];
foreach ($recipients as $recipient) {
$account = $this->entityTypeManager
->getStorage('user')
->load($recipient);
$emails[] = $account
->getEmail();
}
$params['headers']['Bcc'] = implode(',', array_unique($emails));
$this->mail
->mail('mass_contact', 'mass_contact', $configuration['sender_mail'], \Drupal::languageManager()
->getDefaultLanguage()
->getId(), $params);
}
else {
foreach ($recipients as $recipient) {
$account = $this->entityTypeManager
->getStorage('user')
->load($recipient);
$this->mail
->mail('mass_contact', 'mass_contact', $account
->getEmail(), $account
->getPreferredLangcode(), $params);
}
}
}
}