From b500f929219742d78c0223b577e5d3318661832b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kowalewski?= Date: Tue, 30 Jul 2019 16:40:17 +0200 Subject: [PATCH] Create mailBody with only attachments part present --- src/Symfony/Component/Mime/Email.php | 12 ++++++++---- src/Symfony/Component/Mime/Tests/EmailTest.php | 4 ++++ 2 files changed, 12 insertions(+), 4 deletions(-) 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');