You are here

function webform_format_email_address in Webform 7.4

Same name and namespace in other branches
  1. 6.3 webform.module \webform_format_email_address()
  2. 7.3 webform.module \webform_format_email_address()

Given an email address and a name, format an e-mail address.

The address can be the cid of a component with multiple values. When $single is true, a single address is return (the first of any multiples). When not true, an array of addresses is returned.

Note that multiple names could be used with multiple addresses, but this capability is not currently possible with the webform UI. Separate names are only used with the From address, which is always single.

Parameters

$address: The e-mail address.

$name: The name to be used in the formatted address. If the address contains a name in 'Some Name <somename@example.com>' format, $name is ignored.

object $node: The webform node if replacements will be done.

$submission: The webform submission values if replacements will be done.

$encode: Encode the text for use in an e-mail.

$single: Force a single value to be returned, even if a component expands to multiple addresses. This is useful to ensure a single e-mail will be returned for the "From" address.

$format: The e-mail format, defaults to the site-wide setting. May be "short", "long", or NULL for the system default.

$mapping: A mapping array to be applied to the address values.

Return value

string|array The formatted e-mail address -- or addresses (if not $single)

4 calls to webform_format_email_address()
WebformUnitTestCase::test in tests/WebformUnitTestCase.test
The tests.
webform_emails_form in includes/webform.emails.inc
Overview form of all components for this webform.
webform_submission_resend in includes/webform.submissions.inc
Form to resend specific e-mails associated with a submission.
_webform_submission_prepare_mail in includes/webform.submissions.inc
Prepare a submission email for use by webform_submission_send_mail()

File

./webform.module, line 4589
This module provides a simple way to create forms and questionnaires.

Code

function webform_format_email_address($address, $name, $node = NULL, $submission = NULL, $encode = TRUE, $single = TRUE, $format = NULL, $mapping = NULL) {
  if (!isset($format)) {
    $format = webform_variable_get('webform_email_address_format');
  }
  if ($name == 'default') {
    $name = webform_variable_get('webform_default_from_name');
  }
  elseif (is_numeric($name) && isset($node->webform['components'][$name])) {
    if (isset($submission->data[$name])) {
      $component = $node->webform['components'][$name];
      $name = $submission->data[$name];

      // Convert the FROM name to be the label of select lists.
      if (webform_component_implements($component['type'], 'options')) {
        $options = webform_component_invoke($component['type'], 'options', $component);
        foreach ($name as &$one_name) {
          $one_name = isset($options[$one_name]) ? $options[$one_name] : $one_name;
        }

        // Drop PHP reference.
        unset($one_name);
      }
    }
    else {
      $name = t('Value of !component', array(
        '!component' => $node->webform['components'][$name]['name'],
      ));
    }
  }
  elseif (!isset($name)) {
    $name = '';
  }
  if ($address == 'default') {
    $address = webform_variable_get('webform_default_from_address');
  }
  elseif (is_numeric($address) && isset($node->webform['components'][$address])) {
    if (isset($submission->data[$address])) {
      $values = $submission->data[$address];
      $address = array();
      foreach ($values as $value) {
        if (isset($mapping) && isset($mapping[$value])) {
          $value = $mapping[$value];
        }
        $address = array_merge($address, explode(',', $value));
      }
    }
    else {
      $address = t('Value of "!component"', array(
        '!component' => $node->webform['components'][$address]['name'],
      ));
    }
  }

  // Convert single values to an array to simplify processing.
  $address = is_array($address) ? $address : explode(',', $address);
  $name = is_array($name) ? $name : array(
    $name,
  );
  $name_shortage = count($address) - count($name);
  if ($name_shortage > 0) {
    $name += array_fill(count($name), $name_shortage, $name[0]);
  }
  foreach ($address as $key => $individual_address) {
    $individual_address = trim($individual_address);
    $individual_address = webform_replace_tokens($individual_address, $node, $submission);
    $email_parts = webform_parse_email_address($individual_address);
    if ($format == 'long' && !empty($name[$key]) && !strlen($email_parts['name'])) {
      $individual_name = $name[$key];
      $individual_name = webform_replace_tokens($individual_name, $node, $submission);
      if ($encode) {
        $individual_name = mime_header_encode($individual_name);
      }
      $individual_name = trim($individual_name);
      $individual_address = '"' . $individual_name . '" <' . $individual_address . '>';
    }
    $address[$key] = $individual_address;
  }
  return $single ? reset($address) : $address;
}