You are here

class Email in Message Notify 8

Email notifier.

Plugin annotation


@Notifier(
  id = "email",
  title = @Translation("Email"),
  description = @Translation("Send messages via email"),
  viewModes = {
    "mail_subject",
    "mail_body"
  }
)

Hierarchy

Expanded class hierarchy of Email

1 file declares its use of Email
EmailTest.php in tests/src/Unit/Plugin/Notifier/EmailTest.php

File

src/Plugin/Notifier/Email.php, line 26

Namespace

Drupal\message_notify\Plugin\Notifier
View source
class Email extends MessageNotifierBase {

  /**
   * The mail manager service.
   *
   * @var \Drupal\Core\Mail\MailManagerInterface
   */
  protected $mailManager;

  /**
   * Constructs the email notifier plugin.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger
   *   The message_notify logger channel.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\Core\Render\RendererInterface $render
   *   The rendering service.
   * @param \Drupal\message\MessageInterface $message
   *   (optional) The message entity. This is required when sending or
   *   delivering a notification. If not passed to the constructor, use
   *   ::setMessage().
   * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
   *   The mail manager service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelInterface $logger, EntityTypeManagerInterface $entity_type_manager, RendererInterface $render, MessageInterface $message = NULL, MailManagerInterface $mail_manager) {

    // Set configuration defaults.
    $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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  public function deliver(array $output = []) {

    /** @var \Drupal\user\UserInterface $account */
    $account = $this->message
      ->getOwner();
    if (!$this->configuration['mail'] && !$account
      ->id()) {

      // The message has no owner and no mail was passed. This will cause an
      // exception, we just make sure it's a clear one.
      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();
    }

    // The subject in an email can't be with HTML, so strip it.
    $output['mail_subject'] = trim(strip_tags($output['mail_subject']));

    // Pass the message entity along to hook_drupal_mail().
    $output['message_entity'] = $this->message;
    $result = $this->mailManager
      ->mail('message_notify', $this->message
      ->getTemplate()
      ->id(), $mail, $language, $output, $from);
    return $result['result'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Email::$mailManager protected property The mail manager service.
Email::create public static function Creates an instance of the plugin. Overrides MessageNotifierBase::create
Email::deliver public function Deliver a message via the required transport method. Overrides MessageNotifierInterface::deliver
Email::__construct public function Constructs the email notifier plugin. Overrides MessageNotifierBase::__construct
MessageNotifierBase::$entityTypeManager protected property The entity type manager service.
MessageNotifierBase::$logger protected property The logger channel.
MessageNotifierBase::$message protected property The message entity.
MessageNotifierBase::$renderer protected property The rendering service.
MessageNotifierBase::access public function Determine if user can access notifier. Overrides MessageNotifierInterface::access
MessageNotifierBase::postSend public function Save the rendered messages if needed. Invoke watchdog error on failure. Overrides MessageNotifierInterface::postSend
MessageNotifierBase::send public function Entry point to send and process a message. Overrides MessageNotifierInterface::send
MessageNotifierBase::setMessage public function Set the message object for the notifier. Overrides MessageNotifierInterface::setMessage
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.