Skip to content

Commit 50d7a1d

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('[email protected]', 'Fabien, Potencier'); $address->toString(); // Fabien, Potencier <[email protected]> -> Interpreted like two emails ``` After: ```php $address = new Address('[email protected]', 'Fabien, Potencier'); $address->toString(); // "Fabien, Potencier" <[email protected]> ``` Commits ------- 39e9158999 [Mime] Escape commas in address names
2 parents 65a2535 + e2fea3c commit 50d7a1d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

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
/**

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 <[email protected]>', $a->toString());
30+
$this->assertEquals('"Fabien" <[email protected]>', $a->toString());
3131
$this->assertEquals('[email protected]', $a->getEncodedAddress());
3232
}
3333

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

0 commit comments

Comments
 (0)