Skip to content

Commit 2a1e6b0

Browse files
committed
[Mime] allow non-ASCII characters in local part of email
1 parent cd17611 commit 2a1e6b0

File tree

4 files changed

+8
-19
lines changed

4 files changed

+8
-19
lines changed

src/Symfony/Component/Mime/Encoder/IdnAddressEncoder.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,21 @@
1111

1212
namespace Symfony\Component\Mime\Encoder;
1313

14-
use Symfony\Component\Mime\Exception\AddressEncoderException;
15-
1614
/**
1715
* An IDN email address encoder.
1816
*
1917
* Encodes the domain part of an address using IDN. This is compatible will all
2018
* SMTP servers.
2119
*
22-
* This encoder does not support email addresses with non-ASCII characters in
23-
* local-part (the substring before @).
20+
* Note: It leaves the local part as is. In case there are non-ASCII characters
21+
* in the local part then it depends on the SMTP Server if this is supported.
2422
*
2523
* @author Christian Schmidt
2624
*/
2725
final class IdnAddressEncoder implements AddressEncoderInterface
2826
{
2927
/**
3028
* Encodes the domain part of an address using IDN.
31-
*
32-
* @throws AddressEncoderException If local-part contains non-ASCII characters
3329
*/
3430
public function encodeString(string $address): string
3531
{
@@ -38,10 +34,6 @@ public function encodeString(string $address): string
3834
$local = substr($address, 0, $i);
3935
$domain = substr($address, $i + 1);
4036

41-
if (preg_match('/[^\x00-\x7F]/', $local)) {
42-
throw new AddressEncoderException(sprintf('Non-ASCII characters not supported in local-part os "%s".', $address));
43-
}
44-
4537
if (preg_match('/[^\x00-\x7F]/', $domain)) {
4638
$address = sprintf('%s@%s', $local, idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46));
4739
}

src/Symfony/Component/Mime/Tests/Header/MailboxHeaderTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ public function testgetBodyAsString()
5858
$this->assertEquals('Fabien =?'.$header->getCharset().'?Q?P=8Ftencier?= <fabien@symfony.com>', $header->getBodyAsString());
5959
}
6060

61-
public function testUtf8CharsInLocalPartThrows()
61+
public function testUtf8CharsInLocalPart()
6262
{
63-
$this->expectException('Symfony\Component\Mime\Exception\AddressEncoderException');
6463
$header = new MailboxHeader('Sender', new Address('fabïen@symfony.com'));
65-
$header->getBodyAsString();
64+
$this->assertSame('fabïen@symfony.com', $header->getBodyAsString());
6665
}
6766

6867
public function testToString()

src/Symfony/Component/Mime/Tests/Header/MailboxListHeaderTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ public function testUtf8CharsInDomainAreIdnEncoded()
5555
$this->assertEquals(['Chris Corbyn <chris@xn--swftmailer-78a.org>'], $header->getAddressStrings());
5656
}
5757

58-
public function testUtf8CharsInLocalPartThrows()
58+
public function testUtf8CharsInLocalPart()
5959
{
60-
$this->expectException('Symfony\Component\Mime\Exception\AddressEncoderException');
6160
$header = new MailboxListHeader('From', [new Address('chrïs@swiftmailer.org', 'Chris Corbyn')]);
62-
$header->getAddressStrings();
61+
$this->assertSame(['Chris Corbyn <chrïs@swiftmailer.org>'], $header->getAddressStrings());
6362
}
6463

6564
public function testGetMailboxesReturnsNameValuePairs()

src/Symfony/Component/Mime/Tests/Header/PathHeaderTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,10 @@ public function testAddressIsIdnEncoded()
4949
$this->assertEquals('<chris@xn--swftmailer-78a.org>', $header->getBodyAsString());
5050
}
5151

52-
public function testAddressMustBeEncodable()
52+
public function testAddressMustBeEncodableWithUtf8CharsInLocalPart()
5353
{
54-
$this->expectException('Symfony\Component\Mime\Exception\AddressEncoderException');
5554
$header = new PathHeader('Return-Path', new Address('chrïs@swiftmailer.org'));
56-
$header->getBodyAsString();
55+
$this->assertSame('<chrïs@swiftmailer.org>', $header->getBodyAsString());
5756
}
5857

5958
public function testSetBody()

0 commit comments

Comments
 (0)