From ec170f351c0ff8dbdc390c8712dc24c47745abf6 Mon Sep 17 00:00:00 2001 From: ThorstenSuckow Date: Wed, 22 Feb 2023 19:26:15 +0100 Subject: [PATCH] fix: sending emails fail when comma appears in recipient name refs conjoon/php-lib-conjoon#12 --- src/Mail/Client/Data/MailAddress.php | 15 +++++++-- tests/Mail/Client/Data/MailAddressTest.php | 39 +++++++++++++++++++++- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/Mail/Client/Data/MailAddress.php b/src/Mail/Client/Data/MailAddress.php index b3c29bd3..878db75a 100644 --- a/src/Mail/Client/Data/MailAddress.php +++ b/src/Mail/Client/Data/MailAddress.php @@ -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 @@ -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); } @@ -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; + } } diff --git a/tests/Mail/Client/Data/MailAddressTest.php b/tests/Mail/Client/Data/MailAddressTest.php index dea2c6ff..0b805bfa 100644 --- a/tests/Mail/Client/Data/MailAddressTest.php +++ b/tests/Mail/Client/Data/MailAddressTest.php @@ -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 @@ -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", "peter.parker@newyork.com"], + ["\"Parker, Peter\"", "peter.parker@newyork.com"], + ], + [ + ["Parker Peter", "peter.parker@newyork.com"], + ["Parker Peter", "peter.parker@newyork.com"], + ], + [ + ["Parker \" Peter", "peter.parker@newyork.com"], + ["\"Parker \\\" Peter\"", "peter.parker@newyork.com"], + ], + [ + ["Parker ' Peter", "peter.parker@newyork.com"], + ["\"Parker \' Peter\"", "peter.parker@newyork.com"], + ], + [ + ["Parker \"Parker, Peter Peter", "peter.parker@newyork.com"], + ["\"Parker \\\"Parker, Peter Peter\"", "peter.parker@newyork.com"], + ] + ]; + + 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()); + } + } }