Skip to content

Commit 917d779

Browse files
[Mime] Fix body validity check in Email when using Message::setBody()
1 parent ea87c88 commit 917d779

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

Email.php

+1-1
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
}

Tests/EmailTest.php

+56
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('[email protected]')
711+
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('[email protected]')
723+
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('[email protected]')
735+
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('[email protected]')
747+
748+
->setBody(new TextPart('foo'));
749+
750+
$email->ensureValidity();
751+
752+
$this->expectNotToPerformAssertions();
753+
}
698754
}

0 commit comments

Comments
 (0)