If for some reason you cannot compile mailparse into your install of PHP, you will also find an extremely similar function in the Mail_MIME PEAR class, specifically in mimeDecode.php.
(PECL mailparse >= 0.9.0)
mailparse_rfc822_parse_addresses — Разобрать адреса в соответствии с RFC 822
Разбирает список получателей в соответствии с » RFC 822.
Список получателей обычно находится в заголовке To:
.
addresses
Строка, содержащая адреса. Например:
Wez Furlong <[email protected]>, [email protected]
Замечание:
Эта строка не должна содержать название заголовка.
Возвращает ассоциативный массив для каждого получателя со следующими ключами:
display |
Имя получателя. Если эта часть адреса не задана, то будет использовано то же значение,
что и для address .
|
address |
Адрес электронной почты |
is_group |
true , если получатель является группой рассылки и false , если нет. |
Пример #1 Пример использования mailparse_rfc822_parse_addresses()
<?php
$to = 'Wez Furlong <[email protected]>, [email protected]';
var_dump(mailparse_rfc822_parse_addresses($to));
?>
Результат выполнения приведённого примера:
array(2) { [0]=> array(3) { ["display"]=> string(11) "Wez Furlong" ["address"]=> string(15) "[email protected]" ["is_group"]=> bool(false) } [1]=> array(3) { ["display"]=> string(15) "[email protected]" ["address"]=> string(15) "[email protected]" ["is_group"]=> bool(false) } }
If for some reason you cannot compile mailparse into your install of PHP, you will also find an extremely similar function in the Mail_MIME PEAR class, specifically in mimeDecode.php.
<?php
// input: My Test Email <[email protected]>
function get_displayname_from_rfc_email($rfc_email_string) {
// match all words and whitespace, will be terminated by '<'
$name = preg_match('/[\w\s]+/', $rfc_email_string, $matches);
$matches[0] = trim($matches[0]);
return $matches[0];
}
// Output: My Test Email
function get_email_from_rfc_email($rfc_email_string) {
// extract parts between the two parentheses
$mailAddress = preg_match('/(?:<)(.+)(?:>)$/', $rfc_email_string, $matches);
return $matches[1];
}
// Output: [email protected]
?>
An alternative to the mailparse_rfc822_parse_addresses() function is Mail_RFC822::parseAddressList() from Pear:
http://pear.php.net/manual/en/package.mail.mail.php
It parses the string and returns a structured tree of data. Returns a pear_error object if the string is not valid.
Example:
require_once "PEAR.php";
require_once "Mail/RFC822.php";
$addr= "Hi <[email protected]>";
$res= Mail_RFC822::parseAddressList($addr);
if (PEAR::isError($res)) die("NOT VALID: " . $res->getMessage() . "\n");
echo "OK. Data:\n";
print_r($res);