Skip to content

Commit 93467c5

Browse files
committed
bug #39866 [Mime] Escape commas in address names (YaFou)
This PR was merged into the 4.4 branch. Discussion ---------- [Mime] Escape commas in address names | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #39416 | License | MIT | Doc PR | -- <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch 5.x. --> Before: ```php $address = new Address('fabien@symfony.com', 'Fabien, Potencier'); $address->toString(); // Fabien, Potencier <fabien@symfony.com> -> Interpreted like two emails ``` After: ```php $address = new Address('fabien@symfony.com', 'Fabien, Potencier'); $address->toString(); // "Fabien, Potencier" <fabien@symfony.com> ``` Commits ------- 39e9158 [Mime] Escape commas in address names
2 parents fb670df + 39e9158 commit 93467c5

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

src/Symfony/Component/Mailer/Bridge/Amazon/Tests/Transport/SesApiTransportTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public function testSend()
6262
parse_str($options['body'], $content);
6363

6464
$this->assertSame('Hello!', $content['Message_Subject_Data']);
65-
$this->assertSame('Saif Eddin <saif.gmati@symfony.com>', $content['Destination_ToAddresses_member'][0]);
66-
$this->assertSame('Fabien <fabpot@symfony.com>', $content['Source']);
65+
$this->assertSame('"Saif Eddin" <saif.gmati@symfony.com>', $content['Destination_ToAddresses_member'][0]);
66+
$this->assertSame('"Fabien" <fabpot@symfony.com>', $content['Source']);
6767
$this->assertSame('Hello There!', $content['Message_Body_Text_Data']);
6868

6969
$xml = '<SendEmailResponse xmlns="https://email.amazonaws.com/doc/2010-03-31/">

src/Symfony/Component/Mailer/Bridge/Mailgun/Tests/Transport/MailgunApiTransportTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public function testSend()
8282
}
8383

8484
$this->assertStringContainsString('Hello!', $content);
85-
$this->assertStringContainsString('Saif Eddin <saif.gmati@symfony.com>', $content);
86-
$this->assertStringContainsString('Fabien <fabpot@symfony.com>', $content);
85+
$this->assertStringContainsString('"Saif Eddin" <saif.gmati@symfony.com>', $content);
86+
$this->assertStringContainsString('"Fabien" <fabpot@symfony.com>', $content);
8787
$this->assertStringContainsString('Hello There!', $content);
8888

8989
return new MockResponse(json_encode(['id' => 'foobar']), [

src/Symfony/Component/Mailer/Bridge/Postmark/Tests/Transport/PostmarkApiTransportTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public function testSend()
7474
$this->assertStringContainsStringIgnoringCase('X-Postmark-Server-Token: KEY', $options['headers'][1] ?? $options['request_headers'][1]);
7575

7676
$body = json_decode($options['body'], true);
77-
$this->assertSame('Fabien <fabpot@symfony.com>', $body['From']);
78-
$this->assertSame('Saif Eddin <saif.gmati@symfony.com>', $body['To']);
77+
$this->assertSame('"Fabien" <fabpot@symfony.com>', $body['From']);
78+
$this->assertSame('"Saif Eddin" <saif.gmati@symfony.com>', $body['To']);
7979
$this->assertSame('Hello!', $body['Subject']);
8080
$this->assertSame('Hello There!', $body['TextBody']);
8181

src/Symfony/Component/Mime/Address.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,16 @@ public function getEncodedAddress(): string
7878

7979
public function toString(): string
8080
{
81-
return ($n = $this->getName()) ? $n.' <'.$this->getEncodedAddress().'>' : $this->getEncodedAddress();
81+
return ($n = $this->getEncodedName()) ? $n.' <'.$this->getEncodedAddress().'>' : $this->getEncodedAddress();
82+
}
83+
84+
public function getEncodedName(): string
85+
{
86+
if ('' === $this->getName()) {
87+
return '';
88+
}
89+
90+
return sprintf('"%s"', preg_replace('/"/u', '\"', $this->getName()));
8291
}
8392

8493
/**

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testConstructor()
2727
$a = new Address('fabien@symfonï.com', 'Fabien');
2828
$this->assertEquals('Fabien', $a->getName());
2929
$this->assertEquals('fabien@symfonï.com', $a->getAddress());
30-
$this->assertEquals('Fabien <fabien@xn--symfon-nwa.com>', $a->toString());
30+
$this->assertEquals('"Fabien" <fabien@xn--symfon-nwa.com>', $a->toString());
3131
$this->assertEquals('fabien@xn--symfon-nwa.com', $a->getEncodedAddress());
3232
}
3333

@@ -153,4 +153,10 @@ public function fromStringProvider()
153153
],
154154
];
155155
}
156+
157+
public function testEncodeNameIfNameContainsCommas()
158+
{
159+
$address = new Address('fabien@symfony.com', 'Fabien, "Potencier');
160+
$this->assertSame('"Fabien, \"Potencier" <fabien@symfony.com>', $address->toString());
161+
}
156162
}

0 commit comments

Comments
 (0)