Skip to content

Commit

Permalink
Merge pull request #19 from #18
Browse files Browse the repository at this point in the history
fix: the "name"-part of a MailAddress gets quoted multiple times
  • Loading branch information
ThorstenSuckow committed May 3, 2023
2 parents ec170f3 + d8a0989 commit 869cda6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 40 deletions.
9 changes: 4 additions & 5 deletions src/Mail/Client/Data/MailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct(string $address, string $name)
throw new InvalidArgumentException("\"address\" must be set for a MailAddress");
}
$this->address = $address;
$this->name = $this->escapeName($name);
$this->name = $name;
}


Expand Down Expand Up @@ -150,7 +150,7 @@ public static function fromString(string $value): Jsonable
}

$address = $val["address"];
$name = $val["name"] ?? $val["address"];
$name = isset($val["name"]) ? self::escapeName($val["name"]) : $val["address"];

try {
return new self($address, $name);
Expand Down Expand Up @@ -200,20 +200,19 @@ public function toString(): string
*/
public function toJson(JsonStrategy $strategy = null): array
{

return [
'address' => $this->getAddress(),
'name' => $this->getName()
];
}


private function escapeName(string $name): string
private static function escapeName(string $name): string
{
$regex = '/(,|"|\')/m';

if (preg_match($regex, $name) === 1) {
return "\"" . addslashes($name) . "\"";
return '"'.addslashes($name).'"' ;
}
return $name;
}
Expand Down
88 changes: 53 additions & 35 deletions tests/Mail/Client/Data/MailAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,59 @@ public function testToString()
public function testFromString()
{

/**
* @see conjoon/php-lib-conjoon#12
*/
$tests = [
[
["Peter, Parker", "[email protected]"],
["\"Peter, Parker\"", "[email protected]"]
],
[
["Parker Peter", "[email protected]"],
["Parker Peter", "[email protected]"],
],
[
["Parker \" Peter", "[email protected]"],
["\"Parker \\\" Peter\"", "[email protected]"],
],
[
["Parker ' Peter", "[email protected]"],
["\"Parker \' Peter\"", "[email protected]"],
],
[
["Parker \"Parker, Peter Peter", "[email protected]"],
["\"Parker \\\"Parker, Peter Peter\"", "[email protected]"],
],
[
["Schäfer, Peter", "[email protected]"],
["\"Schäfer, Peter\"", "[email protected]"]
],
[
['"Schäfer, Peter"', "[email protected]"],
['"\"Schäfer, Peter\""', "[email protected]"]
],
[
['"Schä"fer, Pe"ter"', "[email protected]"],
['"\"Schä\"fer, Pe\"ter\""', "[email protected]"]
]

];

foreach ($tests as $test) {
[$input, $output] = $test;

$mailAddress = new MailAddress($input[1], $input[0]);

$jsonString = json_encode($mailAddress->toJson());
$fromStringMailAddress = MailAddress::fromString($jsonString);

$this->assertSame($output[1], $fromStringMailAddress->getAddress());
$this->assertSame($output[0], $fromStringMailAddress->getName());
}



$name = "Peter Parker";
$address = "[email protected]";
$mailAddress = new MailAddress($address, $name);
Expand Down Expand Up @@ -166,39 +219,4 @@ public function testCopy()
}


/**
* @see conjoon/php-lib-conjoon#12
*/
public function testForAddressThatNeedToBeSanitized()
{
$tests = [
[
["Parker, Peter", "[email protected]"],
["\"Parker, Peter\"", "[email protected]"],
],
[
["Parker Peter", "[email protected]"],
["Parker Peter", "[email protected]"],
],
[
["Parker \" Peter", "[email protected]"],
["\"Parker \\\" Peter\"", "[email protected]"],
],
[
["Parker ' Peter", "[email protected]"],
["\"Parker \' Peter\"", "[email protected]"],
],
[
["Parker \"Parker, Peter Peter", "[email protected]"],
["\"Parker \\\"Parker, Peter Peter\"", "[email protected]"],
]
];

foreach ($tests as $test) {
[$input, $output] = $test;
$mailAddress = new MailAddress($input[1], $input[0]);
$this->assertSame($output[1], $mailAddress->getAddress());
$this->assertSame($output[0], $mailAddress->getName());
}
}
}

0 comments on commit 869cda6

Please sign in to comment.