Skip to content

[Mime] Fixed Mime\Message::ensureValidity() when a required header is set, but has an empty body #57065

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
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
4 changes: 2 additions & 2 deletions src/Symfony/Component/Mime/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ public function toIterable(): iterable

public function ensureValidity()
{
if (!$this->headers->has('To') && !$this->headers->has('Cc') && !$this->headers->has('Bcc')) {
if (!$this->headers->get('To')?->getBody() && !$this->headers->get('Cc')?->getBody() && !$this->headers->get('Bcc')?->getBody()) {
throw new LogicException('An email must have a "To", "Cc", or "Bcc" header.');
}

if (!$this->headers->has('From') && !$this->headers->has('Sender')) {
if (!$this->headers->get('From')?->getBody() && !$this->headers->get('Sender')?->getBody()) {
throw new LogicException('An email must have a "From" or a "Sender" header.');
}

Expand Down
67 changes: 67 additions & 0 deletions src/Symfony/Component/Mime/Tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,71 @@ public function testSymfonySerialize()
$serialized = $serializer->serialize($e, 'json');
$this->assertSame($expectedJson, json_encode(json_decode($serialized), \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
}

/**
* @dataProvider ensureValidityProvider
*/
public function testEnsureValidity(array $headers, ?string $exceptionClass, ?string $exceptionMessage)
{
if ($exceptionClass) {
$this->expectException($exceptionClass);
$this->expectExceptionMessage($exceptionMessage);
} else {
$this->expectNotToPerformAssertions();
}

$m = new Message();
foreach ($headers as $headerName => $headerValue) {
$m->getHeaders()->addMailboxListHeader($headerName, $headerValue);
}
$m->ensureValidity();
}

public function ensureValidityProvider()
{
return [
'Valid address fields' => [
[
'To' => ['dummy@symfony.com'],
'From' => ['test@symfony.com'],
],
null,
null,
],

'No destination address fields' => [
[
'From' => ['test@symfony.com'],
],
LogicException::class,
'An email must have a "To", "Cc", or "Bcc" header.',
],

'Empty destination address fields' => [
[
'To' => [],
'From' => ['test@symfony.com'],
],
LogicException::class,
'An email must have a "To", "Cc", or "Bcc" header.',
],

'No originator fields' => [
[
'To' => ['dummy@symfony.com'],
],
LogicException::class,
'An email must have a "From" or a "Sender" header.',
],

'Empty originator fields' => [
[
'To' => ['dummy@symfony.com'],
'From' => [],
],
LogicException::class,
'An email must have a "From" or a "Sender" header.',
],
];
}
}
Loading