Skip to content

Commit d43b832

Browse files
committed
Add new accessors to help determine whether to use the SMTPUTF8 extension
1 parent b7ee056 commit d43b832

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/Symfony/Component/Mailer/Envelope.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,17 @@ public function getRecipients(): array
8585
{
8686
return $this->recipients;
8787
}
88+
89+
/**
90+
* @return bool
91+
*/
92+
public function anyAddressHasUnicodeLocalpart(): bool
93+
{
94+
if($this->sender->hasUnicodeLocalpart())
95+
return true;
96+
foreach($this->recipients as $r)
97+
if($r->hasUnicodeLocalpart())
98+
return true;
99+
return false;
100+
}
88101
}

src/Symfony/Component/Mailer/Tests/EnvelopeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ public function testRecipientsFromHeaders()
127127
$this->assertEquals([new Address('[email protected]'), new Address('[email protected]'), new Address('[email protected]')], $e->getRecipients());
128128
}
129129

130+
public function testUnicodeLocalparts()
131+
{
132+
/* dømi means example and is reserved by the .fo registry */
133+
$i = new Address('info@dømi.fo');
134+
$d = new Address('dømi@dømi.fo');
135+
$e = new Envelope($i, [$i]);
136+
$this->assertFalse($e->anyAddressHasUnicodeLocalpart());
137+
$e = new Envelope($i, [$d]);
138+
$this->assertTrue($e->anyAddressHasUnicodeLocalpart());
139+
$e = new Envelope($i, [$i, $d]);
140+
$this->assertTrue($e->anyAddressHasUnicodeLocalpart());
141+
}
142+
130143
public function testRecipientsFromHeadersWithNames()
131144
{
132145
$headers = new Headers();

src/Symfony/Component/Mime/Address.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,27 @@ public static function createArray(array $addresses): array
117117

118118
return $addrs;
119119
}
120+
121+
/**
122+
123+
* Returns true if this address' localpart contains at least one
124+
* non-ASCII character, and false if it is only ASCII (or empty).
125+
*
126+
* This is a helper for Envelope, which has to decide whether to
127+
* the SMTPUTF8 extensions (RFC 6530 and following) for any given
128+
* message.
129+
*
130+
* The SMTPUTF8 extension is strictly required if any address
131+
* contains a non-ASCII character in its localpart. If non-ASCII
132+
* is only used in domains (e.g. horst@freiherr-von-mühlhausen.de)
133+
* then it is possible to to send the message using IDN encoding
134+
* instead of SMTPUTF8. The most common software will display the
135+
* message as intended.
136+
*
137+
* @return bool
138+
*/
139+
public function hasUnicodeLocalpart(): bool
140+
{
141+
return (bool) preg_match( '/[\x80-\xFF].*@/', $this->address);
142+
}
120143
}

src/Symfony/Component/Mime/Tests/AddressTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public function testCreateArray()
8181
$this->assertEquals([$fabien], Address::createArray(['[email protected]']));
8282
}
8383

84+
public function testUnicodeLocalpart()
85+
{
86+
/* dømi means example and is reserved by the .fo registry */
87+
$this->assertFalse((new Address('info@dømi.fo'))->hasUnicodeLocalpart());
88+
$this->assertTrue((new Address('info@dømi.fo'))->hasUnicodeLocalpart());
89+
}
90+
8491
public function testCreateArrayWrongArg()
8592
{
8693
$this->expectException(\TypeError::class);

0 commit comments

Comments
 (0)