Skip to content

Commit be07d1d

Browse files
committed
Merge branch '6.4' into 7.0
* 6.4: fix PHP 7 compatibility [Mime] Fixed `Mime\Message::ensureValidity()` when a required header is set, but has an empty body
2 parents 3adbf11 + 35f0ff4 commit be07d1d

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

Message.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,18 @@ public function toIterable(): iterable
124124

125125
public function ensureValidity(): void
126126
{
127-
if (!$this->headers->has('To') && !$this->headers->has('Cc') && !$this->headers->has('Bcc')) {
127+
$to = (null !== $header = $this->headers->get('To')) ? $header->getBody() : null;
128+
$cc = (null !== $header = $this->headers->get('Cc')) ? $header->getBody() : null;
129+
$bcc = (null !== $header = $this->headers->get('Bcc')) ? $header->getBody() : null;
130+
131+
if (!$to && !$cc && !$bcc) {
128132
throw new LogicException('An email must have a "To", "Cc", or "Bcc" header.');
129133
}
130134

131-
if (!$this->headers->has('From') && !$this->headers->has('Sender')) {
135+
$from = (null !== $header = $this->headers->get('From')) ? $header->getBody() : null;
136+
$sender = (null !== $header = $this->headers->get('Sender')) ? $header->getBody() : null;
137+
138+
if (!$from && !$sender) {
132139
throw new LogicException('An email must have a "From" or a "Sender" header.');
133140
}
134141

Tests/MessageTest.php

+67
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,71 @@ public function testSymfonySerialize()
276276
$serialized = $serializer->serialize($e, 'json');
277277
$this->assertStringMatchesFormat($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' => ['[email protected]'],
305+
'From' => ['[email protected]'],
306+
],
307+
null,
308+
null,
309+
],
310+
311+
'No destination address fields' => [
312+
[
313+
'From' => ['[email protected]'],
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' => ['[email protected]'],
323+
],
324+
LogicException::class,
325+
'An email must have a "To", "Cc", or "Bcc" header.',
326+
],
327+
328+
'No originator fields' => [
329+
[
330+
'To' => ['[email protected]'],
331+
],
332+
LogicException::class,
333+
'An email must have a "From" or a "Sender" header.',
334+
],
335+
336+
'Empty originator fields' => [
337+
[
338+
'To' => ['[email protected]'],
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)