-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce RFC822 parsing if enabled #462
- Loading branch information
Showing
7 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -408,6 +408,24 @@ private function decodeAddresses($values): array { | |
"mailbox" => $mailbox, | ||
"host" => $host, | ||
]; | ||
}elseif (preg_match( | ||
'/^((?P<name>.+)<)(?P<email>[^<]+?)>$/', | ||
$split_address, | ||
$matches | ||
)) { | ||
$name = trim(rtrim($matches["name"])); | ||
if(str_starts_with($name, "\"") && str_ends_with($name, "\"")) { | ||
$name = substr($name, 1, -1); | ||
}elseif(str_starts_with($name, "'") && str_ends_with($name, "'")) { | ||
$name = substr($name, 1, -1); | ||
} | ||
$email = trim(rtrim($matches["email"])); | ||
list($mailbox, $host) = array_pad(explode("@", $email), 2, null); | ||
$addresses[] = (object)[ | ||
"personal" => $name, | ||
"mailbox" => $mailbox, | ||
"host" => $host, | ||
]; | ||
} | ||
} | ||
} | ||
|
@@ -438,7 +456,6 @@ private function parseAddresses($list): array { | |
|
||
if (is_array($list) === false) { | ||
if(is_string($list)) { | ||
// $list = "<[email protected]>" | ||
if (preg_match( | ||
'/^(?:(?P<name>.+)\s)?(?(name)<|<?)(?P<email>[^\s]+?)(?(name)>|>?)$/', | ||
$list, | ||
|
@@ -460,6 +477,32 @@ private function parseAddresses($list): array { | |
"host" => $host, | ||
] | ||
]; | ||
}elseif (preg_match( | ||
'/^((?P<name>.+)<)(?P<email>[^<]+?)>$/', | ||
$list, | ||
$matches | ||
)) { | ||
$name = trim(rtrim($matches["name"])); | ||
$email = trim(rtrim($matches["email"])); | ||
if(str_starts_with($name, "\"") && str_ends_with($name, "\"")) { | ||
$name = substr($name, 1, -1); | ||
}elseif(str_starts_with($name, "'") && str_ends_with($name, "'")) { | ||
$name = substr($name, 1, -1); | ||
} | ||
list($mailbox, $host) = array_pad(explode("@", $email), 2, null); | ||
if($mailbox === ">") { // Fix trailing ">" in malformed mailboxes | ||
$mailbox = ""; | ||
} | ||
if($name === "" && $mailbox === "" && $host === "") { | ||
return $addresses; | ||
} | ||
$list = [ | ||
(object)[ | ||
"personal" => $name, | ||
"mailbox" => $mailbox, | ||
"host" => $host, | ||
] | ||
]; | ||
}else{ | ||
return $addresses; | ||
} | ||
|
@@ -501,14 +544,15 @@ private function parseAddresses($list): array { | |
|
||
if ($address->host == ".SYNTAX-ERROR.") { | ||
$address->host = ""; | ||
}elseif ($address->host == "UNKNOWN") { | ||
$address->host = ""; | ||
} | ||
if ($address->mailbox == "UNEXPECTED_DATA_AFTER_ADDRESS") { | ||
$address->mailbox = ""; | ||
}elseif ($address->mailbox == "MISSING_MAILBOX_TERMINATOR") { | ||
$address->mailbox = ""; | ||
} | ||
|
||
$address->mail = ($address->mailbox && $address->host) ? $address->mailbox . '@' . $address->host : false; | ||
$address->full = ($address->personal) ? $address->personal . ' <' . $address->mail . '>' : $address->mail; | ||
|
||
$addresses[] = new Address($address); | ||
} | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,16 @@ | |
|
||
namespace Tests\fixtures; | ||
|
||
use Webklex\PHPIMAP\Exceptions\AuthFailedException; | ||
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException; | ||
use Webklex\PHPIMAP\Exceptions\ImapBadRequestException; | ||
use Webklex\PHPIMAP\Exceptions\ImapServerErrorException; | ||
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException; | ||
use Webklex\PHPIMAP\Exceptions\MaskNotFoundException; | ||
use Webklex\PHPIMAP\Exceptions\MessageContentFetchingException; | ||
use Webklex\PHPIMAP\Exceptions\ResponseException; | ||
use Webklex\PHPIMAP\Exceptions\RuntimeException; | ||
|
||
/** | ||
* Class EmailAddressTest | ||
* | ||
|
@@ -23,6 +33,16 @@ class EmailAddressTest extends FixtureTestCase { | |
* Test the fixture email_address.eml | ||
* | ||
* @return void | ||
* @throws \ReflectionException | ||
* @throws AuthFailedException | ||
* @throws ConnectionFailedException | ||
* @throws ImapBadRequestException | ||
* @throws ImapServerErrorException | ||
* @throws InvalidMessageDateException | ||
* @throws MaskNotFoundException | ||
* @throws MessageContentFetchingException | ||
* @throws ResponseException | ||
* @throws RuntimeException | ||
*/ | ||
public function testFixture() : void { | ||
$message = $this->getFixture("email_address.eml"); | ||
|
@@ -32,8 +52,8 @@ public function testFixture() : void { | |
self::assertEquals("Hi\r\nHow are you?", $message->getTextBody()); | ||
self::assertFalse($message->hasHTMLBody()); | ||
self::assertFalse($message->date->first()); | ||
self::assertEquals("no_host@UNKNOWN", (string)$message->from); | ||
self::assertEquals("no_host", (string)$message->from); | ||
self::assertEquals("", $message->to); | ||
self::assertEquals("This one: is \"right\" <[email protected]>, No-address@UNKNOWN", $message->cc); | ||
self::assertEquals("This one: is \"right\" <[email protected]>, No-address", (string)$message->cc); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,8 +34,8 @@ public function testFixture() : void { | |
|
||
self::assertEquals("[email protected]", $message->in_reply_to); | ||
self::assertEquals("", $message->from->first()->personal); | ||
self::assertEquals("UNKNOWN", $message->from->first()->host); | ||
self::assertEquals("no_host@UNKNOWN", $message->from->first()->mail); | ||
self::assertEquals("", $message->from->first()->host); | ||
self::assertEquals("no_host", $message->from->first()->mail); | ||
self::assertFalse($message->to->first()); | ||
|
||
self::assertEquals([ | ||
|
@@ -45,7 +45,7 @@ public function testFixture() : void { | |
|
||
self::assertEquals([ | ||
'This one: is "right" <[email protected]>', | ||
'No-address@UNKNOWN' | ||
'No-address' | ||
], $message->cc->map(function($address){ | ||
/** @var \Webklex\PHPIMAP\Address $address */ | ||
return $address->full; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/* | ||
* File: Issue462Test.php | ||
* Category: - | ||
* Author: M.Goldenbaum | ||
* Created: 23.06.23 20:41 | ||
* Updated: - | ||
* | ||
* Description: | ||
* - | ||
*/ | ||
|
||
namespace Tests\issues; | ||
|
||
use Tests\fixtures\FixtureTestCase; | ||
use Webklex\PHPIMAP\Config; | ||
use Webklex\PHPIMAP\Exceptions\AuthFailedException; | ||
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException; | ||
use Webklex\PHPIMAP\Exceptions\ImapBadRequestException; | ||
use Webklex\PHPIMAP\Exceptions\ImapServerErrorException; | ||
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException; | ||
use Webklex\PHPIMAP\Exceptions\MaskNotFoundException; | ||
use Webklex\PHPIMAP\Exceptions\MessageContentFetchingException; | ||
use Webklex\PHPIMAP\Exceptions\ResponseException; | ||
use Webklex\PHPIMAP\Exceptions\RuntimeException; | ||
|
||
class Issue462Test extends FixtureTestCase { | ||
|
||
/** | ||
* @throws RuntimeException | ||
* @throws MessageContentFetchingException | ||
* @throws ResponseException | ||
* @throws ImapBadRequestException | ||
* @throws InvalidMessageDateException | ||
* @throws ConnectionFailedException | ||
* @throws \ReflectionException | ||
* @throws ImapServerErrorException | ||
* @throws AuthFailedException | ||
* @throws MaskNotFoundException | ||
*/ | ||
public function testIssueEmail() { | ||
$config = Config::make(); | ||
$config->set('options.rfc822', false); | ||
$message = $this->getFixture("issue-462.eml", $config); | ||
self::assertSame("Undeliverable: Some subject", (string)$message->subject); | ||
self::assertSame("postmaster@ <sending_domain.tld postmaster@sending_domain.tld>", (string)$message->from->first()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
From: "postmaster@" <sending_domain.tld postmaster@sending_domain.tld> | ||
To: receipent@receipent_domain.tld | ||
Subject: Undeliverable: Some subject | ||
|
||
Test message |