Last active
August 29, 2015 14:00
-
-
Save pschultz/11243394 to your computer and use it in GitHub Desktop.
Parse mailbox headers in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"ext-imap": "*", | |
"swiftmailer/swiftmailer": "~5.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param string $value The value of a mailbox header in wire format | |
* | |
* @throws \Swift_RfcComplianceException if the header cannot be parsed without error | |
* | |
* @return array suitable for \Swift_Message::setTo et. al. | |
*/ | |
function parseMailboxList($value) | |
{ | |
imap_errors(); // this clears the error memory | |
$addresses = array(); | |
$parsed = imap_rfc822_parse_adrlist($value, ''); | |
if ($errors = imap_errors()) { | |
throw new \Swift_RfcComplianceException(reset($error)); | |
} | |
foreach ($parsed as $i) { | |
if (empty($i->host) || empty($i->mailbox)) { | |
continue; | |
} | |
$addresses["{$i->mailbox}@{$i->host}"] = isset($i->personal) ? $i->personal : null; | |
} | |
return $addresses; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment