Skip to content

[Mailer][Mime] Support unicode email addresses #58361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Make `TransportFactoryTestCase` compatible with PHPUnit 10+
* Support unicode email addresses such as "dømi@dømi.fo"

7.1
---
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/Mailer/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,33 @@ public function getRecipients(): array
{
return $this->recipients;
}

/**
* Returns true if any address' localpart contains at least one
* non-ASCII character, and false if all addresses have all-ASCII
* localparts.
*
* This helps to decide whether to the SMTPUTF8 extensions (RFC
* 6530 and following) for any given message.
*
* The SMTPUTF8 extension is strictly required if any address
* contains a non-ASCII character in its localpart. If non-ASCII
* is only used in domains (e.g. horst@freiherr-von-mühlhausen.de)
* then it is possible to to send the message using IDN encoding
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* then it is possible to to send the message using IDN encoding
* then it is possible to send the message using IDN encoding

* instead of SMTPUTF8. The most common software will display the
* message as intended.
*/
public function anyAddressHasUnicodeLocalpart(): bool
{
if ($this->getSender()->hasUnicodeLocalpart()) {
return true;
}
foreach ($this->getRecipients() as $r) {
if ($r->hasUnicodeLocalpart()) {
return true;
}
}

return false;
}
}
15 changes: 14 additions & 1 deletion src/Symfony/Component/Mailer/Tests/EnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function testSenderFromHeadersWithoutFrom()
$this->assertEquals($from, $e->getSender());
}

public function testSenderFromHeadersWithMulitpleHeaders()
public function testSenderFromHeadersWithMultipleHeaders()
{
$headers = new Headers();
$headers->addMailboxListHeader('From', [new Address('from@symfony.com', 'from'), 'some@symfony.com']);
Expand Down Expand Up @@ -127,6 +127,19 @@ public function testRecipientsFromHeaders()
$this->assertEquals([new Address('to@symfony.com'), new Address('cc@symfony.com'), new Address('bcc@symfony.com')], $e->getRecipients());
}

public function testUnicodeLocalparts()
{
/* dømi means example and is reserved by the .fo registry */
$i = new Address('info@dømi.fo');
$d = new Address('dømi@dømi.fo');
$e = new Envelope($i, [$i]);
$this->assertFalse($e->anyAddressHasUnicodeLocalpart());
$e = new Envelope($i, [$d]);
$this->assertTrue($e->anyAddressHasUnicodeLocalpart());
$e = new Envelope($i, [$i, $d]);
$this->assertTrue($e->anyAddressHasUnicodeLocalpart());
}

public function testRecipientsFromHeadersWithNames()
{
$headers = new Headers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Mailer\Tests\Transport\Smtp;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Transport\Smtp\Auth\CramMd5Authenticator;
use Symfony\Component\Mailer\Transport\Smtp\Auth\LoginAuthenticator;
Expand Down Expand Up @@ -62,6 +63,53 @@ public function testExtensibility()
$this->assertContains("RCPT TO:<recipient@example.org> NOTIFY=FAILURE\r\n", $stream->getCommands());
}

public function testSmtpUtf8()
{
$stream = new DummyStream();
$transport = new SmtpUtf8EsmtpTransport(stream: $stream);

$message = new Email();
$message->from('info@dømi.fo');
$message->addTo('dømi@dømi.fo');
$message->text('.');

$transport->send($message);

$this->assertContains("MAIL FROM:<info@xn--dmi-0na.fo> SMTPUTF8\r\n", $stream->getCommands());
$this->assertContains("RCPT TO:<dømi@xn--dmi-0na.fo>\r\n", $stream->getCommands());
}

public function testMissingSmtpUtf8()
{
$stream = new DummyStream();
$transport = new EsmtpTransport(stream: $stream);

$message = new Email();
$message->from('info@dømi.fo');
$message->addTo('dømi@dømi.fo');
$message->text('.');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid addresses: non-ASCII characters not supported in local-part of email.');
$transport->send($message);
}

public function testSmtpUtf8FallbackToIDN()
{
$stream = new DummyStream();
$transport = new EsmtpTransport(stream: $stream);

$message = new Email();
$message->from('info@dømi.fo'); // UTF8 only in the domain
$message->addTo('example@example.com');
$message->text('.');

$transport->send($message);

$this->assertContains("MAIL FROM:<info@xn--dmi-0na.fo>\r\n", $stream->getCommands());
$this->assertContains("RCPT TO:<example@example.com>\r\n", $stream->getCommands());
}

public function testConstructorWithDefaultAuthenticators()
{
$stream = new DummyStream();
Expand Down Expand Up @@ -270,3 +318,17 @@ public function executeCommand(string $command, array $codes): string
return $response;
}
}

class SmtpUtf8EsmtpTransport extends EsmtpTransport
{
public function executeCommand(string $command, array $codes): string
{
$response = parent::executeCommand($command, $codes);

if (str_starts_with($command, 'EHLO ')) {
$response .= "250 SMTPUTF8\r\n";
}

return $response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ private function parseCapabilities(string $ehloResponse): array
return $capabilities;
}

protected function serverSupportsSmtpUtf8(): bool
{
return \array_key_exists('SMTPUTF8', $this->capabilities);
}

private function handleAuth(array $modes): void
{
if (!$this->username) {
Expand Down
15 changes: 12 additions & 3 deletions src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
use Symfony\Component\Mailer\Exception\LogicException;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
Expand Down Expand Up @@ -211,7 +212,7 @@ protected function doSend(SentMessage $message): void

try {
$envelope = $message->getEnvelope();
$this->doMailFromCommand($envelope->getSender()->getEncodedAddress());
$this->doMailFromCommand($envelope->getSender()->getEncodedAddress(), $envelope->anyAddressHasUnicodeLocalpart());
foreach ($envelope->getRecipients() as $recipient) {
$this->doRcptToCommand($recipient->getEncodedAddress());
}
Expand Down Expand Up @@ -244,14 +245,22 @@ protected function doSend(SentMessage $message): void
}
}

protected function serverSupportsSmtpUtf8(): bool
{
return false;
}

private function doHeloCommand(): void
{
$this->executeCommand(\sprintf("HELO %s\r\n", $this->domain), [250]);
}

private function doMailFromCommand(string $address): void
private function doMailFromCommand(string $address, bool $smtputf8): void
{
$this->executeCommand(\sprintf("MAIL FROM:<%s>\r\n", $address), [250]);
if ($smtputf8 && !$this->serverSupportsSmtpUtf8()) {
throw new InvalidArgumentException('Invalid addresses: non-ASCII characters not supported in local-part of email.');
}
$this->executeCommand(\sprintf("MAIL FROM:<%s>%s\r\n", $address, $smtputf8 ? ' SMTPUTF8' : ''), [250]);
}

private function doRcptToCommand(string $address): void
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mailer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"psr/event-dispatcher": "^1",
"psr/log": "^1|^2|^3",
"symfony/event-dispatcher": "^6.4|^7.0",
"symfony/mime": "^6.4|^7.0",
"symfony/mime": "^7.2",
"symfony/service-contracts": "^2.5|^3"
},
"require-dev": {
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/Mime/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,24 @@ public static function createArray(array $addresses): array

return $addrs;
}

/**
* Returns true if this address' localpart contains at least one
* non-ASCII character, and false if it is only ASCII (or empty).
*
* This is a helper for Envelope, which has to decide whether to
* the SMTPUTF8 extensions (RFC 6530 and following) for any given
* message.
*
* The SMTPUTF8 extension is strictly required if any address
* contains a non-ASCII character in its localpart. If non-ASCII
* is only used in domains (e.g. horst@freiherr-von-mühlhausen.de)
* then it is possible to to send the message using IDN encoding
* instead of SMTPUTF8. The most common software will display the
* message as intended.
*/
public function hasUnicodeLocalpart(): bool
{
return (bool) preg_match('/[\x80-\xFF].*@/', $this->address);
}
}
7 changes: 7 additions & 0 deletions src/Symfony/Component/Mime/Tests/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public function testCreateArray()
$this->assertEquals([$fabien], Address::createArray(['fabien@symfony.com']));
}

public function testUnicodeLocalpart()
{
/* dømi means example and is reserved by the .fo registry */
$this->assertFalse((new Address('info@dømi.fo'))->hasUnicodeLocalpart());
$this->assertTrue((new Address('dømi@dømi.fo'))->hasUnicodeLocalpart());
}

public function testCreateArrayWrongArg()
{
$this->expectException(\TypeError::class);
Expand Down