Skip to content

Commit 9e22055

Browse files
committed
bug symfony#57065 [Mime] Fixed Mime\Message::ensureValidity() when a required header is set, but has an empty body (rhertogh)
This PR was submitted for the 6.4 branch but it was squashed and merged into the 5.4 branch instead. Discussion ---------- [Mime] Fixed `Mime\Message::ensureValidity()` when a required header is set, but has an empty body | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT According to [RFC 5322 - Destination Address Fields](https://datatracker.ietf.org/doc/html/rfc5322#section-3.6.3 ) "... Each destination field may have _one or more_ addresses ...". Currently `Message::ensureValidity()` only checks the presence of the required fields in headers which considers the message valid if the field is present but has no body. This PR adds empty checks for the required fields so that an exception will be raised when those fields are all empty. Commits ------- dc91a09 [Mime] Fixed `Mime\Message::ensureValidity()` when a required header is set, but has an empty body
2 parents 3fa0149 + dc91a09 commit 9e22055

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/Symfony/Component/Mime/Message.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ public function toIterable(): iterable
124124

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

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

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,71 @@ public function testSymfonySerialize()
276276
$serialized = $serializer->serialize($e, 'json');
277277
$this->assertSame($expectedJson, json_encode(json_decode($serialized), \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
278278
}
279+
280+
/**
281+
* @dataProvider ensureValidityProvider
282+
*/
283+
public function testEnsureValidity(array $headers, ?string $exceptionClass, ?string $exceptionMessage)
284+
{
285+
if ($exceptionClass) {
286+
$this->expectException($exceptionClass);
287+
$this->expectExceptionMessage($exceptionMessage);
288+
} else {
289+
$this->expectNotToPerformAssertions();
290+
}
291+
292+
$m = new Message();
293+
foreach ($headers as $headerName => $headerValue) {
294+
$m->getHeaders()->addMailboxListHeader($headerName, $headerValue);
295+
}
296+
$m->ensureValidity();
297+
}
298+
299+
public function ensureValidityProvider()
300+
{
301+
return [
302+
'Valid address fields' => [
303+
[
304+
'To' => ['dummy@symfony.com'],
305+
'From' => ['test@symfony.com'],
306+
],
307+
null,
308+
null,
309+
],
310+
311+
'No destination address fields' => [
312+
[
313+
'From' => ['test@symfony.com'],
314+
],
315+
LogicException::class,
316+
'An email must have a "To", "Cc", or "Bcc" header.',
317+
],
318+
319+
'Empty destination address fields' => [
320+
[
321+
'To' => [],
322+
'From' => ['test@symfony.com'],
323+
],
324+
LogicException::class,
325+
'An email must have a "To", "Cc", or "Bcc" header.',
326+
],
327+
328+
'No originator fields' => [
329+
[
330+
'To' => ['dummy@symfony.com'],
331+
],
332+
LogicException::class,
333+
'An email must have a "From" or a "Sender" header.',
334+
],
335+
336+
'Empty originator fields' => [
337+
[
338+
'To' => ['dummy@symfony.com'],
339+
'From' => [],
340+
],
341+
LogicException::class,
342+
'An email must have a "From" or a "Sender" header.',
343+
],
344+
];
345+
}
279346
}

0 commit comments

Comments
 (0)