Skip to content

Commit dfaf4a0

Browse files
committed
Merge branch 'MC-41497' of https://github.com/magento-l3/magento2ce into PR-20210423
2 parents d4884fa + 4eb8594 commit dfaf4a0

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-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, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\Mail\Address;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class AddressConverterTest extends TestCase
16+
{
17+
/**
18+
* @var Address
19+
*/
20+
private $addressMock;
21+
22+
/**
23+
* @var AddressFactory
24+
*/
25+
private $addressFactoryMock;
26+
27+
/**
28+
* @var AddressConverter
29+
*/
30+
private $addressConverter;
31+
32+
protected function setUp(): void
33+
{
34+
$this->addressMock = $this->createMock(Address::class);
35+
$this->addressFactoryMock = $this->createMock(AddressFactory::class);
36+
$this->addressConverter = new AddressConverter($this->addressFactoryMock);
37+
}
38+
39+
/**
40+
* @param string $email
41+
* @param string $name
42+
* @param string $emailExpected
43+
* @param string $nameExpected
44+
* @dataProvider convertDataProvider
45+
*/
46+
public function testConvert(string $email, string $name, string $emailExpected, string $nameExpected)
47+
{
48+
$this->addressFactoryMock->expects($this->once())
49+
->method('create')
50+
->with(['name' => $nameExpected, 'email' => $emailExpected])
51+
->willReturn($this->addressMock);
52+
$address = $this->addressConverter->convert($email, $name);
53+
$this->assertInstanceOf(Address::class, $address);
54+
}
55+
56+
/**
57+
* @return array
58+
*/
59+
public function convertDataProvider(): array
60+
{
61+
return [
62+
[
63+
'email' => 'test@example.com',
64+
'name' => 'Test',
65+
'emailExpected' => 'test@example.com',
66+
'nameExpected' => 'Test'
67+
],
68+
[
69+
'email' => 'tést@example.com',
70+
'name' => 'Test',
71+
'emailExpected' => 'xn--tst-bma@example.com',
72+
'nameExpected' => 'Test'
73+
]
74+
];
75+
}
76+
}

0 commit comments

Comments
 (0)