diff --git a/src/Symfony/Component/Mime/Message.php b/src/Symfony/Component/Mime/Message.php index dbc3ded21bfa5..df7ed1b7944c2 100644 --- a/src/Symfony/Component/Mime/Message.php +++ b/src/Symfony/Component/Mime/Message.php @@ -124,11 +124,18 @@ public function toIterable(): iterable public function ensureValidity() { - if (!$this->headers->get('To')?->getBody() && !$this->headers->get('Cc')?->getBody() && !$this->headers->get('Bcc')?->getBody()) { + $to = (null !== $header = $this->headers->get('To')) ? $header->getBody() : null; + $cc = (null !== $header = $this->headers->get('Cc')) ? $header->getBody() : null; + $bcc = (null !== $header = $this->headers->get('Bcc')) ? $header->getBody() : null; + + if (!$to && !$cc && !$bcc) { throw new LogicException('An email must have a "To", "Cc", or "Bcc" header.'); } - if (!$this->headers->get('From')?->getBody() && !$this->headers->get('Sender')?->getBody()) { + $from = (null !== $header = $this->headers->get('From')) ? $header->getBody() : null; + $sender = (null !== $header = $this->headers->get('Sender')) ? $header->getBody() : null; + + if (!$from && !$sender) { throw new LogicException('An email must have a "From" or a "Sender" header.'); }