PHPMailer\PHPMailer

PHPMailer::parseAddresses()public staticWP 1.0

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.

{} It's a method of the class: PHPMailer{}

No Hooks.

Return

Array.

Usage

$result = PHPMailer::parseAddresses( $addrstr, $useimap, $charset );
$addrstr(string) (required)
The address list string
$useimap(true|false)
Whether to use the IMAP extension to parse the list
Default: true
$charset(string)
The charset to use when decoding the address list string.
Default: self::CHARSET_ISO88591

Notes

Code of PHPMailer::parseAddresses() WP 6.0

public static function parseAddresses($addrstr, $useimap = true, $charset = self::CHARSET_ISO88591)
{
    $addresses = [];
    if ($useimap && function_exists('imap_rfc822_parse_adrlist')) {
        //Use this built-in parser if it's available
        $list = imap_rfc822_parse_adrlist($addrstr, '');
        // Clear any potential IMAP errors to get rid of notices being thrown at end of script.
        imap_errors();
        foreach ($list as $address) {
            if (
                '.SYNTAX-ERROR.' !== $address->host &&
                static::validateAddress($address->mailbox . '@' . $address->host)
            ) {
                //Decode the name part if it's present and encoded
                if (
                    property_exists($address, 'personal') &&
                    //Check for a Mbstring constant rather than using extension_loaded, which is sometimes disabled
                    defined('MB_CASE_UPPER') &&
                    preg_match('/^=\?.*\?=$/s', $address->personal)
                ) {
                    $origCharset = mb_internal_encoding();
                    mb_internal_encoding($charset);
                    //Undo any RFC2047-encoded spaces-as-underscores
                    $address->personal = str_replace('_', '=20', $address->personal);
                    //Decode the name
                    $address->personal = mb_decode_mimeheader($address->personal);
                    mb_internal_encoding($origCharset);
                }

                $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));
                $name = trim($name);
                if (static::validateAddress($email)) {
                    //Check for a Mbstring constant rather than using extension_loaded, which is sometimes disabled
                    //If this name is encoded, decode it
                    if (defined('MB_CASE_UPPER') && preg_match('/^=\?.*\?=$/s', $name)) {
                        $origCharset = mb_internal_encoding();
                        mb_internal_encoding($charset);
                        //Undo any RFC2047-encoded spaces-as-underscores
                        $name = str_replace('_', '=20', $name);
                        //Decode the name
                        $name = mb_decode_mimeheader($name);
                        mb_internal_encoding($origCharset);
                    }
                    $addresses[] = [
                        //Remove any surrounding quotes and spaces from the name
                        'name' => trim($name, '\'" '),
                        'address' => $email,
                    ];
                }
            }
        }
    }

    return $addresses;
}