Skip to content

Commit

Permalink
fix: sending emails fail when comma appears in recipient name
Browse files Browse the repository at this point in the history
refs #12
  • Loading branch information
ThorstenSuckow committed Feb 22, 2023
1 parent 43c2512 commit ec170f3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/Mail/Client/Data/MailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* conjoon
* php-lib-conjoon
* Copyright (C) 2019-2022 Thorsten Suckow-Homberg https://github.com/conjoon/php-lib-conjoon
* Copyright (C) 2019-2023 Thorsten Suckow-Homberg https://github.com/conjoon/php-lib-conjoon
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
Expand Down 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 = $name;
$this->name = $this->escapeName($name);
}


Expand Down Expand Up @@ -206,4 +206,15 @@ public function toJson(JsonStrategy $strategy = null): array
'name' => $this->getName()
];
}


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

if (preg_match($regex, $name) === 1) {
return "\"" . addslashes($name) . "\"";
}
return $name;
}
}
39 changes: 38 additions & 1 deletion tests/Mail/Client/Data/MailAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* conjoon
* php-lib-conjoon
* Copyright (C) 2019-2022 Thorsten Suckow-Homberg https://github.com/conjoon/php-lib-conjoon
* Copyright (C) 2019-2023 Thorsten Suckow-Homberg https://github.com/conjoon/php-lib-conjoon
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -164,4 +164,41 @@ public function testCopy()
$this->assertSame($address1->getName(), $mailAddress->getName());
$this->assertNotSame($address1, $mailAddress);
}


/**
* @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 ec170f3

Please sign in to comment.