Skip to content

Commit ec170f3

Browse files
fix: sending emails fail when comma appears in recipient name
refs #12
1 parent 43c2512 commit ec170f3

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/Mail/Client/Data/MailAddress.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* conjoon
55
* php-lib-conjoon
6-
* Copyright (C) 2019-2022 Thorsten Suckow-Homberg https://github.com/conjoon/php-lib-conjoon
6+
* Copyright (C) 2019-2023 Thorsten Suckow-Homberg https://github.com/conjoon/php-lib-conjoon
77
*
88
* Permission is hereby granted, free of charge, to any person
99
* obtaining a copy of this software and associated documentation
@@ -83,7 +83,7 @@ public function __construct(string $address, string $name)
8383
throw new InvalidArgumentException("\"address\" must be set for a MailAddress");
8484
}
8585
$this->address = $address;
86-
$this->name = $name;
86+
$this->name = $this->escapeName($name);
8787
}
8888

8989

@@ -206,4 +206,15 @@ public function toJson(JsonStrategy $strategy = null): array
206206
'name' => $this->getName()
207207
];
208208
}
209+
210+
211+
private function escapeName(string $name): string
212+
{
213+
$regex = '/(,|"|\')/m';
214+
215+
if (preg_match($regex, $name) === 1) {
216+
return "\"" . addslashes($name) . "\"";
217+
}
218+
return $name;
219+
}
209220
}

tests/Mail/Client/Data/MailAddressTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* conjoon
55
* php-lib-conjoon
6-
* Copyright (C) 2019-2022 Thorsten Suckow-Homberg https://github.com/conjoon/php-lib-conjoon
6+
* Copyright (C) 2019-2023 Thorsten Suckow-Homberg https://github.com/conjoon/php-lib-conjoon
77
*
88
* Permission is hereby granted, free of charge, to any person
99
* obtaining a copy of this software and associated documentation
@@ -164,4 +164,41 @@ public function testCopy()
164164
$this->assertSame($address1->getName(), $mailAddress->getName());
165165
$this->assertNotSame($address1, $mailAddress);
166166
}
167+
168+
169+
/**
170+
* @see conjoon/php-lib-conjoon#12
171+
*/
172+
public function testForAddressThatNeedToBeSanitized()
173+
{
174+
$tests = [
175+
[
176+
["Parker, Peter", "[email protected]"],
177+
["\"Parker, Peter\"", "[email protected]"],
178+
],
179+
[
180+
["Parker Peter", "[email protected]"],
181+
["Parker Peter", "[email protected]"],
182+
],
183+
[
184+
["Parker \" Peter", "[email protected]"],
185+
["\"Parker \\\" Peter\"", "[email protected]"],
186+
],
187+
[
188+
["Parker ' Peter", "[email protected]"],
189+
["\"Parker \' Peter\"", "[email protected]"],
190+
],
191+
[
192+
["Parker \"Parker, Peter Peter", "[email protected]"],
193+
["\"Parker \\\"Parker, Peter Peter\"", "[email protected]"],
194+
]
195+
];
196+
197+
foreach ($tests as $test) {
198+
[$input, $output] = $test;
199+
$mailAddress = new MailAddress($input[1], $input[0]);
200+
$this->assertSame($output[1], $mailAddress->getAddress());
201+
$this->assertSame($output[0], $mailAddress->getName());
202+
}
203+
}
167204
}

0 commit comments

Comments
 (0)