You are here

public function EmailAction::execute in Drupal 9

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

Executes the plugin.

Overrides ExecutableInterface::execute

File

core/lib/Drupal/Core/Action/Plugin/Action/EmailAction.php, line 122

Class

EmailAction
Sends an email message.

Namespace

Drupal\Core\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([
    'mail' => $recipient,
  ]);
  $recipient_account = reset($recipient_accounts);
  if ($recipient_account) {
    $langcode = $recipient_account
      ->getPreferredLangcode();
  }
  else {
    $langcode = $this->languageManager
      ->getDefaultLanguage()
      ->getId();
  }
  $params = [
    'context' => $this->configuration,
  ];
  $message = $this->mailManager
    ->mail('system', 'action_send_email', $recipient, $langcode, $params);

  // Error logging is handled by \Drupal\Core\Mail\MailManager::mail().
  if ($message['result']) {
    $this->logger
      ->notice('Sent email to %recipient', [
      '%recipient' => $recipient,
    ]);
  }
}