You are here

public function EmailWebformHandler::sendMessage in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformHandler/EmailWebformHandler.php \Drupal\webform\Plugin\WebformHandler\EmailWebformHandler::sendMessage()

Sends and logs a webform submission message.

Parameters

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

array $message: An array of message parameters.

Overrides WebformHandlerMessageInterface::sendMessage

3 calls to EmailWebformHandler::sendMessage()
EmailWebformHandler::postDelete in src/Plugin/WebformHandler/EmailWebformHandler.php
Acts on deleted a webform submission before the delete hook is invoked.
EmailWebformHandler::postSave in src/Plugin/WebformHandler/EmailWebformHandler.php
Acts on a saved webform submission before the insert or update hook is invoked.
ScheduleEmailWebformHandler::scheduleMessage in modules/webform_scheduled_email/src/Plugin/WebformHandler/ScheduleEmailWebformHandler.php
Schedule the sending of an email.

File

src/Plugin/WebformHandler/EmailWebformHandler.php, line 1148

Class

EmailWebformHandler
Emails a webform submission.

Namespace

Drupal\webform\Plugin\WebformHandler

Code

public function sendMessage(WebformSubmissionInterface $webform_submission, array $message) {
  $to = $message['to_mail'];
  $from = $message['from_mail'];

  // Remove less than (<) and greater (>) than from name.
  $message['from_name'] = preg_replace('/[<>]/', '', $message['from_name']);
  if (!empty($message['from_name'])) {
    $from = Mail::formatDisplayName($message['from_name']) . ' <' . $from . '>';
  }
  $current_langcode = $this->languageManager
    ->getCurrentLanguage()
    ->getId();

  // Don't send the message if To, CC, and BCC is empty.
  if (!$this
    ->hasRecipient($webform_submission, $message)) {
    if ($this->configuration['debug']) {
      $t_args = [
        '%form' => $this
          ->getWebform()
          ->label(),
        '%handler' => $this
          ->label(),
      ];
      $this
        ->messenger()
        ->addWarning($this
        ->t('%form: Email not sent for %handler handler because a <em>To</em>, <em>CC</em>, or <em>BCC</em> email was not provided.', $t_args), TRUE);
    }
    return;
  }

  // Render body using webform email message (wrapper) template.
  $build = [
    '#theme' => 'webform_email_message_' . ($this->configuration['html'] ? 'html' : 'text'),
    '#message' => [
      'body' => is_string($message['body']) ? Markup::create($message['body']) : $message['body'],
    ] + $message,
    '#webform_submission' => $webform_submission,
    '#handler' => $this,
  ];
  $theme_name = $this->configuration['theme_name'];
  $message['body'] = trim((string) $this->themeManager
    ->renderPlain($build, $theme_name));

  // Html body needs to be Markup so that relative URLs are converted
  // to absolute.
  // @see \Drupal\Core\Mail\MailManager::doMail
  if ($this->configuration['html']) {
    $message['body'] = Markup::create($message['body']);
  }

  // Send message.
  $key = $this
    ->getWebform()
    ->id() . '_' . $this
    ->getHandlerId();

  // Remove webform_submission and handler to prevent memory limit
  // issues during testing.
  if (drupal_valid_test_ua()) {
    unset($message['webform_submission'], $message['handler']);
  }

  // Append additional custom parameters.
  if (!empty($this->configuration['parameters'])) {
    $message += $this
      ->replaceTokens($this->configuration['parameters'], $webform_submission);
  }

  // Remove parameters.
  unset($message['parameters']);
  $result = $this->mailManager
    ->mail('webform', $key, $to, $current_langcode, $message, $from);
  if ($webform_submission
    ->getWebform()
    ->hasSubmissionLog()) {

    // Log detailed message to the 'webform_submission' log.
    $context = [
      '@from_name' => $message['from_name'],
      '@from_mail' => $message['from_mail'],
      '@to_mail' => $message['to_mail'],
      '@subject' => $message['subject'],
      'link' => $webform_submission
        ->id() ? $webform_submission
        ->toLink($this
        ->t('View'))
        ->toString() : NULL,
      'webform_submission' => $webform_submission,
      'handler_id' => $this
        ->getHandlerId(),
      'operation' => 'sent email',
    ];
    $this
      ->getLogger('webform_submission')
      ->notice("'@subject' sent to '@to_mail' from '@from_name' [@from_mail]'.", $context);
  }
  else {

    // Log general message to the 'webform' log.
    $context = [
      '@form' => $this
        ->getWebform()
        ->label(),
      '@title' => $this
        ->label(),
      'link' => $this
        ->getWebform()
        ->toLink($this
        ->t('Edit'), 'handlers')
        ->toString(),
    ];
    $this
      ->getLogger('webform')
      ->notice('@form webform sent @title email.', $context);
  }

  // Debug by displaying send email onscreen.
  if ($this->configuration['debug']) {
    $t_args = [
      '%from_name' => $message['from_name'],
      '%from_mail' => $message['from_mail'],
      '%to_mail' => $message['to_mail'],
      '%subject' => $message['subject'],
    ];
    $this
      ->messenger()
      ->addWarning($this
      ->t("%subject sent to %to_mail from %from_name [%from_mail].", $t_args), TRUE);
    $debug_message = $this
      ->buildDebugMessage($webform_submission, $message);
    $this
      ->messenger()
      ->addWarning($this->themeManager
      ->renderPlain($debug_message), TRUE);
  }
  return $result['send'];
}