Create mailBody with only attachments part present

This commit is contained in:
Radosław Kowalewski 2019-07-30 16:40:17 +02:00 committed by Fabien Potencier
parent 85827f32e4
commit b500f92921
2 changed files with 12 additions and 4 deletions

View File

@ -423,12 +423,12 @@ class Email extends Message
*/ */
private function generateBody(): AbstractPart private function generateBody(): AbstractPart
{ {
if (null === $this->text && null === $this->html) { [$htmlPart, $attachmentParts, $inlineParts] = $this->prepareParts();
throw new LogicException('A message must have a text and/or an HTML part.'); 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); $part = null === $this->text ? null : new TextPart($this->text, $this->textCharset);
[$htmlPart, $attachmentParts, $inlineParts] = $this->prepareParts();
if (null !== $htmlPart) { if (null !== $htmlPart) {
if (null !== $part) { if (null !== $part) {
$part = new AlternativePart($part, $htmlPart); $part = new AlternativePart($part, $htmlPart);
@ -442,7 +442,11 @@ class Email extends Message
} }
if ($attachmentParts) { if ($attachmentParts) {
$part = new MixedPart($part, ...$attachmentParts); if ($part) {
$part = new MixedPart($part, ...$attachmentParts);
} else {
$part = new MixedPart(...$attachmentParts);
}
} }
return $part; return $part;

View File

@ -284,6 +284,10 @@ class EmailTest extends TestCase
$e->html('html content'); $e->html('html content');
$this->assertEquals(new MixedPart($html, $att), $e->getBody()); $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 = new Email();
$e->html('html content'); $e->html('html content');
$e->text('text content'); $e->text('text content');