function EmailAction::execute

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Action/Plugin/Action/EmailAction.php \Drupal\Core\Action\Plugin\Action\EmailAction::execute()
  2. 8.9.x core/lib/Drupal/Core/Action/Plugin/Action/EmailAction.php \Drupal\Core\Action\Plugin\Action\EmailAction::execute()
  3. 10 core/lib/Drupal/Core/Action/Plugin/Action/EmailAction.php \Drupal\Core\Action\Plugin\Action\EmailAction::execute()

Overrides ExecutableInterface::execute

File

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

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
            ->info('Sent email to %recipient', [
            '%recipient' => $recipient,
        ]);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.