vendor/symfony/mime/Message.php line 120

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Mime;
  11. use Symfony\Component\Mime\Exception\LogicException;
  12. use Symfony\Component\Mime\Header\Headers;
  13. use Symfony\Component\Mime\Part\AbstractPart;
  14. use Symfony\Component\Mime\Part\TextPart;
  15. /**
  16.  * @author Fabien Potencier <[email protected]>
  17.  */
  18. class Message extends RawMessage
  19. {
  20.     private Headers $headers;
  21.     private ?AbstractPart $body;
  22.     public function __construct(Headers $headers nullAbstractPart $body null)
  23.     {
  24.         $this->headers $headers ? clone $headers : new Headers();
  25.         $this->body $body;
  26.     }
  27.     public function __clone()
  28.     {
  29.         $this->headers = clone $this->headers;
  30.         if (null !== $this->body) {
  31.             $this->body = clone $this->body;
  32.         }
  33.     }
  34.     /**
  35.      * @return $this
  36.      */
  37.     public function setBody(AbstractPart $body null): static
  38.     {
  39.         if (\func_num_args()) {
  40.             trigger_deprecation('symfony/mime''6.2''Calling "%s()" without any arguments is deprecated, pass null explicitly instead.'__METHOD__);
  41.         }
  42.         $this->body $body;
  43.         return $this;
  44.     }
  45.     public function getBody(): ?AbstractPart
  46.     {
  47.         return $this->body;
  48.     }
  49.     /**
  50.      * @return $this
  51.      */
  52.     public function setHeaders(Headers $headers): static
  53.     {
  54.         $this->headers $headers;
  55.         return $this;
  56.     }
  57.     public function getHeaders(): Headers
  58.     {
  59.         return $this->headers;
  60.     }
  61.     public function getPreparedHeaders(): Headers
  62.     {
  63.         $headers = clone $this->headers;
  64.         if (!$headers->has('From')) {
  65.             if (!$headers->has('Sender')) {
  66.                 throw new LogicException('An email must have a "From" or a "Sender" header.');
  67.             }
  68.             $headers->addMailboxListHeader('From', [$headers->get('Sender')->getAddress()]);
  69.         }
  70.         if (!$headers->has('MIME-Version')) {
  71.             $headers->addTextHeader('MIME-Version''1.0');
  72.         }
  73.         if (!$headers->has('Date')) {
  74.             $headers->addDateHeader('Date', new \DateTimeImmutable());
  75.         }
  76.         // determine the "real" sender
  77.         if (!$headers->has('Sender') && \count($froms $headers->get('From')->getAddresses()) > 1) {
  78.             $headers->addMailboxHeader('Sender'$froms[0]);
  79.         }
  80.         if (!$headers->has('Message-ID')) {
  81.             $headers->addIdHeader('Message-ID'$this->generateMessageId());
  82.         }
  83.         // remove the Bcc field which should NOT be part of the sent message
  84.         $headers->remove('Bcc');
  85.         return $headers;
  86.     }
  87.     public function toString(): string
  88.     {
  89.         if (null === $body $this->getBody()) {
  90.             $body = new TextPart('');
  91.         }
  92.         return $this->getPreparedHeaders()->toString().$body->toString();
  93.     }
  94.     public function toIterable(): iterable
  95.     {
  96.         if (null === $body $this->getBody()) {
  97.             $body = new TextPart('');
  98.         }
  99.         yield $this->getPreparedHeaders()->toString();
  100.         yield from $body->toIterable();
  101.     }
  102.     public function ensureValidity()
  103.     {
  104.         if (!$this->headers->has('To') && !$this->headers->has('Cc') && !$this->headers->has('Bcc')) {
  105.             throw new LogicException('An email must have a "To", "Cc", or "Bcc" header.');
  106.         }
  107.         if (!$this->headers->has('From') && !$this->headers->has('Sender')) {
  108.             throw new LogicException('An email must have a "From" or a "Sender" header.');
  109.         }
  110.         parent::ensureValidity();
  111.     }
  112.     public function generateMessageId(): string
  113.     {
  114.         if ($this->headers->has('Sender')) {
  115.             $sender $this->headers->get('Sender')->getAddress();
  116.         } elseif ($this->headers->has('From')) {
  117.             $sender $this->headers->get('From')->getAddresses()[0];
  118.         } else {
  119.             throw new LogicException('An email must have a "From" or a "Sender" header.');
  120.         }
  121.         return bin2hex(random_bytes(16)).strstr($sender->getAddress(), '@');
  122.     }
  123.     public function __serialize(): array
  124.     {
  125.         return [$this->headers$this->body];
  126.     }
  127.     public function __unserialize(array $data): void
  128.     {
  129.         [$this->headers$this->body] = $data;
  130.     }
  131. }