Skip to content

Instantly share code, notes, and snippets.

@pschultz
Last active August 29, 2015 14:00
Show Gist options
  • Save pschultz/11243394 to your computer and use it in GitHub Desktop.
Save pschultz/11243394 to your computer and use it in GitHub Desktop.
Parse mailbox headers in PHP
{
"require": {
"ext-imap": "*",
"swiftmailer/swiftmailer": "~5.0"
}
}
<?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