PHPMailer\PHPMailer

PHPMailer::parseAddressespublic 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 and the deprecated $useimap argument is truthy. Note that quotes in the name part are removed.

Method of the class: PHPMailer{}

No Hooks.

Returns

Array.

Usage

$result = PHPMailer::parseAddresses( $addrstr, $useimap, $charset );
$addrstr(string) (required)
The address list string.
$useimap(true|false|null)
Deprecated in PHPMailer 6.11.0. Truthy values request the deprecated IMAP parser and trigger a deprecation warning.
Default: null
$charset(string)
The charset to use when decoding the address list string.
Default: self::CHARSET_ISO88591

Notes

PHPMailer::parseAddresses() code WP 7.1

public static function parseAddresses($addrstr, $useimap = null, $charset = self::CHARSET_ISO88591)
{
    if ($useimap == true) {
        trigger_error(self::lang('deprecated_argument') . '$useimap', E_USER_DEPRECATED);
    }
    $addresses = [];
    if ($useimap == true && function_exists('imap_rfc822_parse_adrlist')) {
        //Use this built-in parser if it's available
        // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.imap_rfc822_parse_adrlistRemoved -- wrapped in function_exists()
        $list = imap_rfc822_parse_adrlist($addrstr, '');
        // Clear any potential IMAP errors to get rid of notices being thrown at end of script.
        // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.imap_errorsRemoved -- wrapped in function_exists()
        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 maybe encoded
                if (
                    property_exists($address, 'personal')
                    && is_string($address->personal)
                    && $address->personal !== ''
                ) {
                    $address->personal = static::decodeHeader($address->personal, $charset);
                }

                $addresses[] = [
                    'name' => (property_exists($address, 'personal') ? $address->personal : ''),
                    'address' => $address->mailbox . '@' . $address->host,
                ];
            }
        }
    } else {
        //Use this simpler parser
        $addresses = static::parseSimplerAddresses($addrstr, $charset);
    }

    return $addresses;
}