Skip to content

Commit dfc116d

Browse files
committed
MC-41497: [Contact us] Invalid header value detected error when French characters such as "é", "è" on the email address
1 parent 9a68d68 commit dfc116d

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

lib/internal/Magento/Framework/Mail/AddressConverter.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function __construct(
4040
*/
4141
public function convert(string $email, ?string $name = null): Address
4242
{
43+
$email = $this->convertEmailUserToAscii($email);
4344
return $this->addressFactory->create(
4445
[
4546
'name' => $name,
@@ -48,6 +49,26 @@ public function convert(string $email, ?string $name = null): Address
4849
);
4950
}
5051

52+
/**
53+
* Convert email user to ascii
54+
*
55+
* @param string $email
56+
* @return string
57+
*/
58+
private function convertEmailUserToAscii(string $email): string
59+
{
60+
if (preg_match('/^(.+)@([^@]+)$/', $email, $matches)) {
61+
$user = $matches[1];
62+
$hostname = $matches[2];
63+
$userEncoded = idn_to_ascii($user);
64+
if ($userEncoded == $user) {
65+
return $email;
66+
}
67+
$email = sprintf('%s@%s', $userEncoded, $hostname);
68+
}
69+
return $email;
70+
}
71+
5172
/**
5273
* Converts array to list of MailAddresses
5374
*
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Mail\Test\Unit;
9+
10+
use Magento\Framework\Mail\AddressConverter;
11+
use Magento\Framework\Mail\AddressFactory;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\ObjectManager\Config\Config;
14+
use Magento\Framework\ObjectManager\Factory\Dynamic\Developer;
15+
use Magento\Framework\ObjectManager\Relations\Runtime;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class AddressConverterTest extends TestCase
19+
{
20+
/**
21+
* @var ObjectManager
22+
*/
23+
private $objectManager;
24+
25+
protected function setUp(): void
26+
{
27+
$config = new Config(
28+
new Runtime()
29+
);
30+
$factory = new Developer(
31+
$config
32+
);
33+
$this->objectManager = new ObjectManager($factory, $config);
34+
}
35+
36+
/**
37+
* @param string $email
38+
* @param string $name
39+
* @param string $emailExpected
40+
* @param string $nameExpected
41+
* @dataProvider convertDataProvider
42+
*/
43+
public function testConvert(string $email, string $name, string $emailExpected, string $nameExpected)
44+
{
45+
$addressFactory = new AddressFactory($this->objectManager);
46+
$addressConverter = new AddressConverter($addressFactory);
47+
$address = $addressConverter->convert($email, $name);
48+
$this->assertSame($emailExpected, $address->getEmail());
49+
$this->assertSame($nameExpected, $address->getName());
50+
}
51+
52+
/**
53+
* @return array
54+
*/
55+
public function convertDataProvider(): array
56+
{
57+
return [
58+
[
59+
'email' => '[email protected]',
60+
'name' => 'Test',
61+
'emailExpected' => '[email protected]',
62+
'nameExpected' => 'Test'
63+
],
64+
[
65+
'email' => '[email protected]',
66+
'name' => 'Test',
67+
'emailExpected' => '[email protected]',
68+
'nameExpected' => 'Test'
69+
]
70+
];
71+
}
72+
}

0 commit comments

Comments
 (0)