public static function PHPMailer::parseAddresses

5.x PHPMailer.php public static PHPMailer::parseAddresses($addrstr, $useimap = true)

Parse and validate a string containing one or more RFC822-style comma-separated email addresses of the form "display name <address>" into an array of name/address pairs. Uses the imap_rfc822_parse_adrlist function if the IMAP extension is available. Note that quotes in the name part are removed.

Parameters

string $addrstr The address list string:

bool $useimap Whether to use the IMAP extension to parse the list:

Return value

array

See also

http://www.andrew.cmu.edu/user/agreen1/testing/mrbs/web/Mail/RFC822.php A more careful implementation

File

inc/PHPMailer/src/PHPMailer.php, line 1126

Class

PHPMailer
PHPMailer - PHP email creation and transport class.

Namespace

PHPMailer\PHPMailer

Code

public static function parseAddresses($addrstr, $useimap = true) 
 {
  $addresses = [];
  if ($useimap and function_exists('imap_rfc822_parse_adrlist')) {
    //Use this built-in parser if it's available
    $list = imap_rfc822_parse_adrlist($addrstr, '');
    foreach ($list as $address) {
      if ('.SYNTAX-ERROR.' != $address->host) {
        if (static::validateAddress($address->mailbox . '@' . $address->host)) {
          $addresses [] = [
            'name' => (property_exists($address, 'personal') ? $address->personal : ''),
            'address' => $address->mailbox . '@' . $address->host,
          ];
        }
      }
    }
  }
  else {
    //Use this simpler parser
    $list = explode(',', $addrstr);
    foreach ($list as $address) {
      $address = trim($address);
      //Is there a separate name part?
      if (strpos($address, '<') === false) {
        //No separate name, just use the whole thing
        if (static::validateAddress($address)) {
          $addresses [] = [
            'name' => '',
            'address' => $address,
          ];
        }
      }
      else {
        list($name, $email) = explode('<', $address);
        $email = trim(str_replace('>', '', $email));
        if (static::validateAddress($email)) {
          $addresses [] = [
            'name' => trim(str_replace(['"', "'"], '', $name)),
            'address' => $email,
          ];
        }
      }
    }
  }

  return $addresses;
}