function phpmailer_parse_address in PHPMailer 5.2
Same name and namespace in other branches
- 8.3 phpmailer.module \phpmailer_parse_address()
- 5 phpmailer.module \phpmailer_parse_address()
- 6.3 phpmailer.module \phpmailer_parse_address()
- 6 phpmailer.module \phpmailer_parse_address()
- 6.2 phpmailer.module \phpmailer_parse_address()
- 7.4 phpmailer.module \phpmailer_parse_address()
- 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
2 calls to phpmailer_parse_address()
- mimemail_phpmailer_send in includes/phpmailer.mimemail.inc 
- Send out an e-mail.
- 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 = "/^(?<name>.*)\\s<(?<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)) {
      $parsed[] = array(
        'mail' => $matches['address'],
        'name' => $matches['name'],
      );
    }
    else {
      $parsed[] = array(
        'mail' => trim($email, '<>'),
        'name' => '',
      );
    }
  }
  return $parsed;
}