8000 [Mime] Escape commas in address names by YaFou · Pull Request #39866 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Mime] Escape commas in address names #39866

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 1 commit into from
Mar 12, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public function testSend()
parse_str($options['body'], $content);

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

$xml = '<SendEmailResponse xmlns="https://email.amazonaws.com/doc/2010-03-31/">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public function testSend()
}

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

return new MockResponse(json_encode(['id' => 'foobar']), [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public function testSend()
$this->assertStringContainsStringIgnoringCase('X-Postmark-Server-Token: KEY', $options['headers'][1] ?? $options['request_headers'][1]);

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

Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Component/Mime/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,16 @@ public function getEncodedAddress(): string

public function toString(): string
{
return ($n = $this->getName()) ? $n.' <'.$this->getEncodedAddress().'>' : $this->getEncodedAddress();
return ($n = $this->getEncodedName()) ? $n.' <'.$this->getEncodedAddress().'>' : $this->getEncodedAddress();
}

public function getEncodedName(): string
{
if ('' === $this->getName()) {
return '';
}

return sprintf('"%s"', preg_replace('/"/u', '\"', $this->getName()));
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/Mime/Tests/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testConstructor()
$a = new Address('fabien@symfonï.com', 'Fabien');
$this->assertEquals('Fabien', $a->getName());
$this->assertEquals('fabien@symfonï.com', $a->getAddress());
$this->assertEquals('Fabien <fabien@xn--symfon-nwa.com>', $a->toString());
$this->assertEquals('"Fabien" <fabien@xn--symfon-nwa.com>', $a->toString());
$this->assertEquals('fabien@xn--symfon-nwa.com', $a->getEncodedAddress());
}

Expand Down Expand Up @@ -153,4 +153,10 @@ public function fromStringProvider()
],
];
}

public function testEncodeNameIfNameContainsCommas()
{
$address = new Address('fabien@symfony.com', 'Fabien, "Potencier');
$this->assertSame('"Fabien, \"Potencier" <fabien@symfony.com>', $address->toString());
}
}
0