Skip to content

[Mime] Fix body validity check in Email when using Message::setBody() #59598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mime/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function ensureValidity()

private function ensureBodyValid(): void
{
if (null === $this->text && null === $this->html && !$this->attachments) {
if (null === $this->text && null === $this->html && !$this->attachments && null === parent::getBody()) {
throw new LogicException('A message must have a text or an HTML part or attachments.');
}
}
Expand Down
56 changes: 56 additions & 0 deletions src/Symfony/Component/Mime/Tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -695,4 +695,60 @@ public function testEmailsWithAttachmentsWhichAreAFileInstanceCanBeUnserialized(
$this->assertCount(1, $attachments);
$this->assertStringContainsString('foo_bar_xyz_123', $attachments[0]->getBody());
}

public function testInvalidBodyWithEmptyEmail()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('A message must have a text or an HTML part or attachments.');

(new Email())->ensureValidity();
}

public function testBodyWithTextIsValid()
{
$email = new Email();
$email->to('test@example.com')
->from('test@example.com')
->text('foo');

$email->ensureValidity();

$this->expectNotToPerformAssertions();
}

public function testBodyWithHtmlIsValid()
{
$email = new Email();
$email->to('test@example.com')
->from('test@example.com')
->html('foo');

$email->ensureValidity();

$this->expectNotToPerformAssertions();
}

public function testEmptyBodyWithAttachmentsIsValid()
{
$email = new Email();
$email->to('test@example.com')
->from('test@example.com')
->addPart(new DataPart('foo'));

$email->ensureValidity();

$this->expectNotToPerformAssertions();
}

public function testSetBodyIsValid()
{
$email = new Email();
$email->to('test@example.com')
->from('test@example.com')
->setBody(new TextPart('foo'));

$email->ensureValidity();

$this->expectNotToPerformAssertions();
}
}