You are here

protected function EmailWebformHandler::getMessageEmails in Webform 8.5

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

Get message to, cc, bcc, and from email addresses.

Parameters

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

string $configuration_name: The email configuration name. (i.e. to, cc, bcc, or from)

string $configuration_value: The email configuration value.

Return value

array An array of email addresses and/or tokens.

1 call to EmailWebformHandler::getMessageEmails()
EmailWebformHandler::getMessage in src/Plugin/WebformHandler/EmailWebformHandler.php
Get a fully populated email for a webform submission.

File

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

Class

EmailWebformHandler
Emails a webform submission.

Namespace

Drupal\webform\Plugin\WebformHandler

Code

protected function getMessageEmails(WebformSubmissionInterface $webform_submission, $configuration_name, $configuration_value) {
  $emails = [];

  // Get element from token and make sure the element has #options.
  $element_name = $this
    ->getElementKeyFromToken($configuration_value);
  $element = $element_name ? $this->webform
    ->getElement($element_name) : NULL;
  $element_has_options = $element && isset($element['#options']) ? TRUE : FALSE;

  // Check that email handle configuration has email #options.
  $email_has_options = !empty($this->configuration[$configuration_name . '_options']) ? TRUE : FALSE;

  // Get emails from options.
  if ($element_has_options && $email_has_options) {
    $email_options = WebformOptionsHelper::decodeConfig($this->configuration[$configuration_name . '_options']);

    // Set default email address.
    if (!empty($email_options[static::DEFAULT_OPTION])) {
      $emails[] = $email_options[static::DEFAULT_OPTION];
    }

    // Get submission email addresses as an array.
    $options_element_value = $webform_submission
      ->getElementData($element_name);
    if (is_array($options_element_value)) {
      $options_values = $options_element_value;
    }
    elseif ($options_element_value) {
      $options_values = [
        $options_element_value,
      ];
    }

    // Set empty email address.
    if (empty($options_values)) {
      if (!empty($email_options[static::EMPTY_OPTION])) {
        $emails[] = $email_options[static::EMPTY_OPTION];
      }
    }
    else {
      foreach ($options_values as $option_value) {
        if (!empty($email_options[$option_value])) {
          $emails[] = $email_options[$option_value];
        }
        elseif (!empty($email_options[static::OTHER_OPTION])) {
          $emails[] = $email_options[static::OTHER_OPTION];
        }
      }
    }
  }
  else {
    $emails[] = $configuration_value;
  }

  // Implode unique emails and tokens.
  $emails = implode(',', array_unique($emails));

  // Add user role email addresses to 'To', 'CC', and 'BCC'.
  // IMPORTANT: This is the only place where user email addresses can be
  // used as tokens. This prevents the webform module from being used to
  // spam users or worse… expose user email addresses to malicious users.
  if (in_array($configuration_name, [
    'to',
    'cc',
    'bcc',
  ])) {
    $roles = $this->configFactory
      ->get('webform.settings')
      ->get('mail.roles');
    $token_data = [];
    $token_data['webform_role'] = $roles;
    if ($this->moduleHandler
      ->moduleExists('webform_access')) {
      $token_data['webform_access'] = $webform_submission;
    }
    if ($this->moduleHandler
      ->moduleExists('webform_group')) {
      $token_data['webform_group'] = $webform_submission;
    }
    $emails = $this
      ->replaceTokens($emails, $webform_submission, $token_data);
  }

  // Resplit emails to make sure that emails are unique.
  $emails = preg_split('/\\s*,\\s*/', $emails);

  // Remove all empty email addresses.
  $emails = array_filter($emails);

  // Make sure all email addresses are unique.
  $emails = array_unique($emails);

  // Sort email addresses to make it easier to debug queuing and/or sending
  // issues.
  asort($emails);
  return $emails;
}