View source
<?php
namespace Drupal\social_user\Plugin\AdvancedQueue\JobType;
use Drupal\advancedqueue\Job;
use Drupal\advancedqueue\JobResult;
use Drupal\advancedqueue\Plugin\AdvancedQueue\JobType\JobTypeBase;
use Drupal\Component\Utility\EmailValidatorInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\social_queue_storage\Entity\QueueStorageEntity;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserMailQueueJob extends JobTypeBase implements ContainerFactoryPluginInterface {
use LoggerChannelTrait;
protected $mailManager;
protected $storage;
protected $connection;
protected $languageManager;
protected $emailValidator;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MailManagerInterface $mail_manager, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation, Connection $database, LanguageManagerInterface $language_manager, EmailValidatorInterface $email_validator) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->mailManager = $mail_manager;
$this->storage = $entity_type_manager;
$this->connection = $database;
$this
->setStringTranslation($string_translation);
$this->languageManager = $language_manager;
$this->emailValidator = $email_validator;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.mail'), $container
->get('entity_type.manager'), $container
->get('string_translation'), $container
->get('database'), $container
->get('language_manager'), $container
->get('email.validator'));
}
public function process(Job $job) {
try {
$data = $job
->getPayload();
if (self::validateQueueItem($data)) {
$queue_storage = $this->storage
->getStorage('queue_storage_entity')
->load($data['mail']);
if ($queue_storage
->bundle() === 'email') {
if ($data['users']) {
$users = $this->storage
->getStorage('user')
->loadMultiple($data['users']);
foreach ($users as $user) {
if ($user
->getEmail()) {
$this
->sendMail($user
->getEmail(), $user
->language()
->getId(), $queue_storage, $user
->getDisplayName());
}
}
}
if (!empty($data['user_mail_addresses'])) {
foreach ($data['user_mail_addresses'] as $mail_address) {
if ($this->emailValidator
->isValid($mail_address['email_address'])) {
$this
->sendMail($mail_address['email_address'], $this->languageManager
->getDefaultLanguage()
->getId(), $queue_storage, $mail_address['display_name']);
}
}
}
$queue_storage
->setFinished(TRUE);
$queue_storage
->save();
return JobResult::success('The user mail job was successfully finished.');
}
}
$this
->getLogger('user_email_queue')
->error('The Job was not finished correctly, no error thrown.');
return JobResult::failure('The Job was not finished correctly, no error thrown.');
} catch (\Exception $e) {
$this
->getLogger('user_email_queue')
->error($e
->getMessage());
return JobResult::failure($e
->getMessage());
}
}
protected function sendMail(string $user_mail, string $langcode, QueueStorageEntity $mail_params, $display_name = NULL) {
$subject = $mail_params
->get('field_subject')
->getValue();
$message = $mail_params
->get('field_message')
->getValue();
$reply_to = $mail_params
->get('field_reply_to')
->getValue();
$reply = NULL;
if (!empty($reply_to)) {
$reply = $reply_to[0]['value'];
}
$context = [
'subject' => $subject[0]['value'],
'message' => $message[0]['value'],
];
if ($display_name) {
$context['display_name'] = $display_name;
}
$this->mailManager
->mail('system', 'action_send_email', $user_mail, $langcode, [
'context' => $context,
], $reply);
}
private static function validateQueueItem(array $data) {
return isset($data['mail']) && (isset($data['users']) || isset($data['user_mail_addresses']));
}
}