Skip to content

Commit

Permalink
Merge branch '7.1' into 7.2
Browse files Browse the repository at this point in the history
* 7.1:
  Revert "fix PHP 7 compatibility"
  fix PHP 7 compatibility
  [Mime] Fixed `Mime\Message::ensureValidity()` when a required header is set, but has an empty body
  • Loading branch information
fabpot committed May 29, 2024
2 parents bed9c36 + 31c597f commit 9f77ba3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
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(): void
{
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->assertStringMatchesFormat($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' => ['[email protected]'],
'From' => ['[email protected]'],
],
null,
null,
],

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

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

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

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

0 comments on commit 9f77ba3

Please sign in to comment.