diff --git a/src/Symfony/Component/Mime/Email.php b/src/Symfony/Component/Mime/Email.php index 3fbebc461f..1bcdc8a1cd 100644 --- a/src/Symfony/Component/Mime/Email.php +++ b/src/Symfony/Component/Mime/Email.php @@ -423,12 +423,12 @@ class Email extends Message */ private function generateBody(): AbstractPart { - if (null === $this->text && null === $this->html) { - throw new LogicException('A message must have a text and/or an HTML part.'); + [$htmlPart, $attachmentParts, $inlineParts] = $this->prepareParts(); + if (null === $this->text && null === $this->html && !$attachmentParts) { + throw new LogicException('A message must have a text or an HTML part or attachments.'); } $part = null === $this->text ? null : new TextPart($this->text, $this->textCharset); - [$htmlPart, $attachmentParts, $inlineParts] = $this->prepareParts(); if (null !== $htmlPart) { if (null !== $part) { $part = new AlternativePart($part, $htmlPart); @@ -442,7 +442,11 @@ class Email extends Message } if ($attachmentParts) { - $part = new MixedPart($part, ...$attachmentParts); + if ($part) { + $part = new MixedPart($part, ...$attachmentParts); + } else { + $part = new MixedPart(...$attachmentParts); + } } return $part; diff --git a/src/Symfony/Component/Mime/Tests/EmailTest.php b/src/Symfony/Component/Mime/Tests/EmailTest.php index 1d45cab9f4..d8a982add7 100644 --- a/src/Symfony/Component/Mime/Tests/EmailTest.php +++ b/src/Symfony/Component/Mime/Tests/EmailTest.php @@ -284,6 +284,10 @@ class EmailTest extends TestCase $e->html('html content'); $this->assertEquals(new MixedPart($html, $att), $e->getBody()); + $e = new Email(); + $e->attach($file); + $this->assertEquals(new MixedPart($att), $e->getBody()); + $e = new Email(); $e->html('html content'); $e->text('text content');