You are here

public function EmailAction::execute in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/action/src/Plugin/Action/EmailAction.php \Drupal\action\Plugin\Action\EmailAction::execute()

Executes the plugin.

Overrides ExecutableInterface::execute

File

core/modules/action/src/Plugin/Action/EmailAction.php, line 126
Contains \Drupal\action\Plugin\Action\EmailAction.

Class

EmailAction
Sends an email message.

Namespace

Drupal\action\Plugin\Action

Code

public function execute($entity = NULL) {
  if (empty($this->configuration['node'])) {
    $this->configuration['node'] = $entity;
  }
  $recipient = PlainTextOutput::renderFromHtml($this->token
    ->replace($this->configuration['recipient'], $this->configuration));

  // If the recipient is a registered user with a language preference, use
  // the recipient's preferred language. Otherwise, use the system default
  // language.
  $recipient_accounts = $this->storage
    ->loadByProperties(array(
    'mail' => $recipient,
  ));
  $recipient_account = reset($recipient_accounts);
  if ($recipient_account) {
    $langcode = $recipient_account
      ->getPreferredLangcode();
  }
  else {
    $langcode = $this->languageManager
      ->getDefaultLanguage()
      ->getId();
  }
  $params = array(
    'context' => $this->configuration,
  );
  if ($this->mailManager
    ->mail('system', 'action_send_email', $recipient, $langcode, $params)) {
    $this->logger
      ->notice('Sent email to %recipient', array(
      '%recipient' => $recipient,
    ));
  }
  else {
    $this->logger
      ->error('Unable to send email to %recipient', array(
      '%recipient' => $recipient,
    ));
  }
}