Skip to content

Commit

Permalink
[BUGFIX] Use Address::create instead of new
Browse files Browse the repository at this point in the history
The usage of Address::create instead of a plain new ensures
compatibility with all Symfony releases. Since Symfony 5.2.6 the
behavior was changed to always return email addresses properly
quoted but this change broke our Mailer.

Resolves: #93843
Related: #93831
Releases: master, 10.4
Change-Id: Ia8d35c220be0ba46cf75dec51585158760fad26d
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/68975
Tested-by: core-ci <typo3@b13.com>
Tested-by: Simon Gilli <typo3@gilbertsoft.org>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Simon Gilli <typo3@gilbertsoft.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
  • Loading branch information
gilbertsoft authored and lolli42 committed May 4, 2021
1 parent 709127c commit d16bf8a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 22 deletions.
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -62,8 +62,8 @@
"symfony/expression-language": "^4.4 || ^5.0",
"symfony/finder": "^4.4 || ^5.0",
"symfony/http-foundation": "^4.4 || ^5.0",
"symfony/mailer": "^4.4.21 || ^5.2.6",
"symfony/mime": "^4.4.21 || ^5.2.6",
"symfony/mailer": "^4.4 || ^5.0",
"symfony/mime": "^4.4 || ^5.0",
"symfony/polyfill-intl-icu": "^1.6",
"symfony/polyfill-intl-idn": "^1.10",
"symfony/polyfill-mbstring": "^1.2",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions typo3/sysext/core/Classes/Mail/MailMessage.php
Expand Up @@ -247,7 +247,7 @@ public function setReadReceiptTo(string $address): self
protected function convertNamedAddress(...$args): array
{
if (isset($args[1])) {
return [new Address($args[0], $args[1])];
return [new Address($args[0], \trim($args[1], ' \'"'))];
}
if (is_string($args[0]) || is_array($args[0])) {
return $this->convertAddresses($args[0]);
Expand All @@ -271,7 +271,7 @@ protected function convertAddresses($addresses): array
if (is_numeric($email) || ctype_digit($email)) {
$newAddresses[] = Address::create($name);
} else {
$newAddresses[] = new Address($email, $name);
$newAddresses[] = new Address($email, \trim($name, ' \'"'));
}
}

Expand Down
97 changes: 82 additions & 15 deletions typo3/sysext/core/Tests/Unit/Mail/MailMessageTest.php
Expand Up @@ -93,8 +93,16 @@ public function setReturnPathWorksAsExpected(): void
public function setSenderAddressDataProvider(): array
{
return [
'address without name' => ['admin@typo3.com', null, 'admin@typo3.com'],
'address with name' => ['admin@typo3.com', 'Admin', '"Admin" <admin@typo3.com>'],
'address without name' => [
'admin@typo3.com', null, [
['admin@typo3.com']
]
],
'address with name' => [
'admin@typo3.com', 'Admin', [
['admin@typo3.com', 'Admin', '<admin@typo3.com>']
]
],
];
}

Expand All @@ -105,23 +113,66 @@ public function setSenderAddressDataProvider(): array
* @param string $name
* @param string $expectedString
*/
public function setSenderWorksAsExpected($address, $name, $expectedString): void
public function setSenderWorksAsExpected($address, $name, array $expectedAddresses): void
{
$this->subject->setSender($address, $name);
self::assertInstanceOf(Address::class, $this->subject->getSender());
self::assertSame($address, $this->subject->getSender()->getAddress());
self::assertSame($expectedString, $this->subject->getSender()->toString());
$this->assertCorrectAddresses([$this->subject->getSender()], $expectedAddresses);
}

public function globalSetAddressDataProvider(): array
{
return [
'address without name' => ['admin@typo3.com', null, ['admin@typo3.com']],
'address with name' => ['admin@typo3.com', 'Admin', ['"Admin" <admin@typo3.com>']],
'multiple addresses without name' => [['admin@typo3.com', 'system@typo3.com'], null, ['admin@typo3.com', 'system@typo3.com']],
'address as array' => [['admin@typo3.com' => 'Admin'], null, ['"Admin" <admin@typo3.com>']],
'multiple addresses as array' => [['admin@typo3.com' => 'Admin', 'system@typo3.com' => 'System'], null, ['"Admin" <admin@typo3.com>', '"System" <system@typo3.com>']],
'multiple addresses as array mixed' => [['admin@typo3.com' => 'Admin', 'it@typo3.com', 'system@typo3.com' => 'System'], null, ['"Admin" <admin@typo3.com>', 'it@typo3.com', '"System" <system@typo3.com>']],
'address without name' => [
'admin@typo3.com', null, [
['admin@typo3.com']
]
],
'address with name' => [
'admin@typo3.com', 'Admin', [
['admin@typo3.com', 'Admin', '<admin@typo3.com>']
]
],
'address with name enclosed in quotes' => [
'admin@typo3.com', '"Admin"', [
['admin@typo3.com', 'Admin', '<admin@typo3.com>']
]
],
'multiple addresses without name' => [
[
'admin@typo3.com',
'system@typo3.com'
], null, [
['admin@typo3.com'],
['system@typo3.com']
]
],
'address as array' => [
['admin@typo3.com' => 'Admin'], null, [
['admin@typo3.com', 'Admin', '<admin@typo3.com>']
]
],
'multiple addresses as array' => [
[
'admin@typo3.com' => 'Admin',
'system@typo3.com' => 'System'
], null, [
['admin@typo3.com', 'Admin', '<admin@typo3.com>'],
['system@typo3.com', 'System', '<system@typo3.com>']
]
],
'multiple addresses as array mixed' => [
[
'admin@typo3.com' => 'Admin',
'it@typo3.com',
'system@typo3.com' => 'System'
], null, [
['admin@typo3.com', 'Admin', '<admin@typo3.com>'],
['it@typo3.com'],
['system@typo3.com', 'System', '<system@typo3.com>']
]
],
];
}

Expand Down Expand Up @@ -203,9 +254,21 @@ public function setBccToWorksAsExpected($address, $name, array $expectedAddresse
public function globalAddAddressDataProvider(): array
{
return [
'address without name' => ['admin@typo3.com', null, ['admin@typo3.com']],
'address with name' => ['admin@typo3.com', 'Admin', ['"Admin" <admin@typo3.com>']],
'address as array' => [['admin@typo3.com' => 'Admin'], null, ['"Admin" <admin@typo3.com>']],
'address without name' => [
'admin@typo3.com', null, [
['admin@typo3.com']
]
],
'address with name' => [
'admin@typo3.com', 'Admin', [
['admin@typo3.com', 'Admin', '<admin@typo3.com>']
]
],
'address as array' => [
['admin@typo3.com' => 'Admin'], null, [
['admin@typo3.com', 'Admin', '<admin@typo3.com>']
]
],
];
}

Expand Down Expand Up @@ -317,8 +380,12 @@ protected function assertCorrectAddresses(array $dataToCheck, array $expectedAdd
{
self::assertIsArray($dataToCheck);
self::assertCount(count($expectedAddresses), $dataToCheck);
foreach ($dataToCheck as $singleAddress) {
self::assertContains($singleAddress->toString(), $expectedAddresses);
foreach ($expectedAddresses as $key => $expectedAddress) {
self::assertIsArray($expectedAddress);
self::assertSame($expectedAddress[0], $dataToCheck[$key]->getAddress());
foreach ($expectedAddress as $expectedAddressPart) {
self::assertStringContainsString($expectedAddressPart, $dataToCheck[$key]->toString());
}
}
}
}
4 changes: 2 additions & 2 deletions typo3/sysext/core/composer.json
Expand Up @@ -49,8 +49,8 @@
"symfony/expression-language": "^4.4 || ^5.0",
"symfony/finder": "^4.4 || ^5.0",
"symfony/http-foundation": "^4.4 || ^5.0",
"symfony/mailer": "^4.4.21 || ^5.2.6",
"symfony/mime": "^4.4.21 || ^5.2.6",
"symfony/mailer": "^4.4 || ^5.0",
"symfony/mime": "^4.4 || ^5.0",
"symfony/polyfill-intl-icu": "^1.6",
"symfony/polyfill-intl-idn": "^1.10",
"symfony/polyfill-mbstring": "^1.2",
Expand Down

0 comments on commit d16bf8a

Please sign in to comment.