You are here

function phpmailer_parse_address in PHPMailer 8.3

Same name and namespace in other branches
  1. 5.2 phpmailer.module \phpmailer_parse_address()
  2. 5 phpmailer.module \phpmailer_parse_address()
  3. 6.3 phpmailer.module \phpmailer_parse_address()
  4. 6 phpmailer.module \phpmailer_parse_address()
  5. 6.2 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()
DrupalPHPMailer::mail in src/Plugin/Mail/DrupalPHPMailer.php
Sends an e-mail message composed by drupal_mail().
mimemail_phpmailer_send in includes/phpmailer.mimemail.inc
Send out an e-mail.
PHPMailerUnitTest::testAddressParser in tests/src/Unit/PHPMailerUnitTest.php
Tests e-mail address extraction using phpmailer_parse_address().

File

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

Code

function phpmailer_parse_address($string) {
  $parsed = [];

  // 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_callback('(".*?(?<!\\\\)")', '_phpmailer_stack', $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_callback('(\\x01)', '_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[] = [
        'mail' => $matches['address'],
        'name' => Unicode::mimeHeaderDecode(stripslashes($matches['name'])),
      ];
    }
    else {
      $parsed[] = [
        'mail' => trim($email, '<>'),
        'name' => '',
      ];
    }
  }
  return $parsed;
}