You are here

function phpmailer_parse_address in PHPMailer 6.2

Same name and namespace in other branches
  1. 8.3 phpmailer.module \phpmailer_parse_address()
  2. 5.2 phpmailer.module \phpmailer_parse_address()
  3. 5 phpmailer.module \phpmailer_parse_address()
  4. 6.3 phpmailer.module \phpmailer_parse_address()
  5. 6 phpmailer.module \phpmailer_parse_address()
  6. 7.4 phpmailer.module \phpmailer_parse_address()
  7. 7.3 phpmailer.module \phpmailer_parse_address()

Extract address and optional display name of an e-mail address.

Parameters

$string: A string containing one or more valid e-mail address(es) separated with commas.

Return value

An array containing all found e-mail addresses split into mail and name.

See also

http://tools.ietf.org/html/rfc5322#section-3.4

3 calls to phpmailer_parse_address()
mimemail_phpmailer_send in includes/phpmailer.mimemail.inc
Send out an e-mail.
PHPMailerUnitTestCase::testAddressParser in tests/phpmailer.test
Test e-mail address extraction using phpmailer_parse_address().
phpmailer_send in includes/phpmailer.drupal.inc
Send out an e-mail.

File

./phpmailer.module, line 111
Integrates the PHPMailer library for SMTP e-mail delivery.

Code

function phpmailer_parse_address($string) {
  $parsed = array();

  // The display name may contain commas (3.4). Extract all quoted strings
  // (3.2.4) to a stack and replace them with a placeholder to prevent
  // splitting at wrong places.
  $string = preg_replace('/(".*?(?<!\\\\)")/e', '_phpmailer_stack("$1")', $string);

  // Build a regex that matches a name-addr (3.4).
  // @see valid_email_address()
  $user = '[a-zA-Z0-9_\\-\\.\\+\\^!#\\$%&*+\\/\\=\\?\\`\\|\\{\\}~\']+';
  $domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.?)+';
  $ipv4 = '[0-9]{1,3}(?:\\.[0-9]{1,3}){3}';
  $ipv6 = '[0-9a-fA-F]{1,4}(?:\\:[0-9a-fA-F]{1,4}){7}';
  $address = "{$user}@(?:{$domain}|(?:\\[(?:{$ipv4}|{$ipv6})\\]))";
  $adr_rx = "/^(?P<name>.*)\\s<(?P<address>{$address})>\$/";

  // Split string into multiple parts and process each.
  foreach (explode(',', $string) as $email) {

    // Re-inject stripped placeholders.
    $email = preg_replace('/\\x01/e', '_phpmailer_stack()', trim($email));

    // Check if it's a name-addr or a plain address (3.4).
    if (preg_match($adr_rx, $email, $matches)) {

      // PHPMailer expects an unencoded display name.
      $parsed[] = array(
        'mail' => $matches['address'],
        'name' => mime_header_decode(stripslashes($matches['name'])),
      );
    }
    else {
      $parsed[] = array(
        'mail' => trim($email, '<>'),
        'name' => '',
      );
    }
  }
  return $parsed;
}