public function EmailAction::execute

Executes the plugin.

Overrides ExecutableInterface::execute

File

drupal/core/modules/action/lib/Drupal/action/Plugin/Action/EmailAction.php, line 76
Contains \Drupal\action\Plugin\Action\EmailAction.

Class

EmailAction
Sends an e-mail message.

Namespace

Drupal\action\Plugin\Action

Code

public function execute($entity = NULL) {
  if (empty($this->configuration['node'])) {
    $this->configuration['node'] = $entity;
  }
  $recipient = $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->storageController
    ->loadByProperties(array(
    'mail' => $recipient,
  ));
  $recipient_account = reset($recipient_accounts);
  if ($recipient_account) {
    $langcode = user_preferred_langcode($recipient_account);
  }
  else {
    $langcode = language_default()->langcode;
  }
  $params = array(
    'context' => $this->configuration,
  );
  if (drupal_mail('system', 'action_send_email', $recipient, $langcode, $params)) {
    watchdog('action', 'Sent email to %recipient', array(
      '%recipient' => $recipient,
    ));
  }
  else {
    watchdog('error', 'Unable to send email to %recipient', array(
      '%recipient' => $recipient,
    ));
  }
}