Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
francescobianco committed Jul 5, 2022
1 parent 64f2862 commit b6209da
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,8 @@ test-polyfill:
test-parse-headers:
@docker-compose run --rm phpunit tests --filter PolyfillTest::testRfc822ParseHeaders

test-parse-adrlist:
@docker-compose run --rm phpunit tests --filter PolyfillTest::testRfc822ParseAdrList

test-special:
@docker-compose run --rm phpunit tests --filter HeaderInfoTest::testSanitizeAddress
9 changes: 7 additions & 2 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,22 @@ public static function isValidImap2Connection($imap)
return Connection::isValid($imap);
}

public static function getAddressObjectList($addressList)
public static function getAddressObjectList($addressList, $defaultHostname = 'UNKNOWN')
{
$addressObjectList = [];
foreach ($addressList as $toAddress) {
$email = explode('@', $toAddress->getEmail());

$addressObject = (object) [
'mailbox' => $email[0],
'host' => $email[1],
'host' => $email[1] ?? $defaultHostname,
];

$personal = $toAddress->getName();
if ($personal) {
$addressObject->personal = $personal;
}

$addressObjectList[] = $addressObject;
}

Expand Down
9 changes: 7 additions & 2 deletions src/Polyfill.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ public static function qPrint($string)
return $string;
}

public static function rfc822ParseAdrList($string)
public static function rfc822ParseAdrList($string, $defaultHostname)
{
return $string;
$message = Message::from('To: '.$string, false);

return Functions::getAddressObjectList(
$message->getHeader(HeaderConsts::TO)->getAddresses(),
$defaultHostname
);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/PolyfillTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,25 @@ public function testRfc822ParseHeaders()
$this->assertEquals($headers1, $headers2);
}
}

public function testRfc822ParseAdrList()
{
$fixtures = [
'Sender <[email protected]>, [email protected], root',
];

$defaultHostname = 'default.host';
foreach ($fixtures as $addresses) {
$addressObjectList1 = imap_rfc822_parse_adrlist($addresses, $defaultHostname);
$addressObjectList2 = Polyfill::rfc822ParseAdrList($addresses, $defaultHostname);

#file_put_contents('a1.json', json_encode($addressObjectList1, JSON_PRETTY_PRINT));
#file_put_contents('a2.json', json_encode($addressObjectList2, JSON_PRETTY_PRINT));

#var_dump($addressObjectList1);
#var_dump($addressObjectList2);

$this->assertEquals($addressObjectList1, $addressObjectList2);
}
}
}

0 comments on commit b6209da

Please sign in to comment.