Skip to content

Commit c09342e

Browse files
committed
bug #59598 [Mime] Fix body validity check in Email when using Message::setBody() (alexandre-daubois)
This PR was merged into the 6.4 branch. Discussion ---------- [Mime] Fix body validity check in `Email` when using `Message::setBody()` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #59582 | License | MIT Commits ------- b983d1b [Mime] Fix body validity check in `Email` when using `Message::setBody()`
2 parents b6f477c + b983d1b commit c09342e

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/Symfony/Component/Mime/Email.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ public function ensureValidity()
416416

417417
private function ensureBodyValid(): void
418418
{
419-
if (null === $this->text && null === $this->html && !$this->attachments) {
419+
if (null === $this->text && null === $this->html && !$this->attachments && null === parent::getBody()) {
420420
throw new LogicException('A message must have a text or an HTML part or attachments.');
421421
}
422422
}

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,4 +695,60 @@ public function testEmailsWithAttachmentsWhichAreAFileInstanceCanBeUnserialized(
695695
$this->assertCount(1, $attachments);
696696
$this->assertStringContainsString('foo_bar_xyz_123', $attachments[0]->getBody());
697697
}
698+
699+
public function testInvalidBodyWithEmptyEmail()
700+
{
701+
$this->expectException(\LogicException::class);
702+
$this->expectExceptionMessage('A message must have a text or an HTML part or attachments.');
703+
704+
(new Email())->ensureValidity();
705+
}
706+
707+
public function testBodyWithTextIsValid()
708+
{
709+
$email = new Email();
710+
$email->to('test@example.com')
711+
->from('test@example.com')
712+
->text('foo');
713+
714+
$email->ensureValidity();
715+
716+
$this->expectNotToPerformAssertions();
717+
}
718+
719+
public function testBodyWithHtmlIsValid()
720+
{
721+
$email = new Email();
722+
$email->to('test@example.com')
723+
->from('test@example.com')
724+
->html('foo');
725+
726+
$email->ensureValidity();
727+
728+
$this->expectNotToPerformAssertions();
729+
}
730+
731+
public function testEmptyBodyWithAttachmentsIsValid()
732+
{
733+
$email = new Email();
734+
$email->to('test@example.com')
735+
->from('test@example.com')
736+
->addPart(new DataPart('foo'));
737+
738+
$email->ensureValidity();
739+
740+
$this->expectNotToPerformAssertions();
741+
}
742+
743+
public function testSetBodyIsValid()
744+
{
745+
$email = new Email();
746+
$email->to('test@example.com')
747+
->from('test@example.com')
748+
->setBody(new TextPart('foo'));
749+
750+
$email->ensureValidity();
751+
752+
$this->expectNotToPerformAssertions();
753+
}
698754
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy