View source
<?php
namespace Drupal\message_notify\Plugin\Notifier;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\message\MessageInterface;
use Drupal\message_notify\Exception\MessageNotifyException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Email extends MessageNotifierBase {
protected $mailManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelInterface $logger, EntityTypeManagerInterface $entity_type_manager, RendererInterface $render, MessageInterface $message = NULL, MailManagerInterface $mail_manager) {
$configuration += [
'mail' => FALSE,
'language override' => FALSE,
'from' => FALSE,
];
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger, $entity_type_manager, $render, $message);
$this->mailManager = $mail_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MessageInterface $message = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('logger.channel.message_notify'), $container
->get('entity_type.manager'), $container
->get('renderer'), $message, $container
->get('plugin.manager.mail'));
}
public function deliver(array $output = []) {
$account = $this->message
->getOwner();
if (!$this->configuration['mail'] && !$account
->id()) {
throw new MessageNotifyException('It is not possible to send a Message to an anonymous owner. You may set an owner using ::setOwner() or pass a "mail" to the $options array.');
}
$mail = $this->configuration['mail'] ?: $account
->getEmail();
$from = $this->configuration['from'] ?: NULL;
if (!$this->configuration['language override']) {
$language = $account
->getPreferredLangcode();
}
else {
$language = $this->message
->language()
->getId();
}
$output['mail_subject'] = trim(strip_tags($output['mail_subject']));
$output['message_entity'] = $this->message;
$result = $this->mailManager
->mail('message_notify', $this->message
->getTemplate()
->id(), $mail, $language, $output, $from);
return $result['result'];
}
}