Message.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 <fabien@symfony.com>
  17. */
  18. class Message extends RawMessage
  19. {
  20. private $headers;
  21. private $body;
  22. public function __construct(Headers $headers = null, AbstractPart $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)
  38. {
  39. $this->body = $body;
  40. return $this;
  41. }
  42. public function getBody(): ?AbstractPart
  43. {
  44. return $this->body;
  45. }
  46. /**
  47. * @return $this
  48. */
  49. public function setHeaders(Headers $headers)
  50. {
  51. $this->headers = $headers;
  52. return $this;
  53. }
  54. public function getHeaders(): Headers
  55. {
  56. return $this->headers;
  57. }
  58. public function getPreparedHeaders(): Headers
  59. {
  60. $headers = clone $this->headers;
  61. if (!$headers->has('From')) {
  62. if (!$headers->has('Sender')) {
  63. throw new LogicException('An email must have a "From" or a "Sender" header.');
  64. }
  65. $headers->addMailboxListHeader('From', [$headers->get('Sender')->getAddress()]);
  66. }
  67. $headers->addTextHeader('MIME-Version', '1.0');
  68. if (!$headers->has('Date')) {
  69. $headers->addDateHeader('Date', new \DateTimeImmutable());
  70. }
  71. // determine the "real" sender
  72. if (!$headers->has('Sender') && \count($froms = $headers->get('From')->getAddresses()) > 1) {
  73. $headers->addMailboxHeader('Sender', $froms[0]);
  74. }
  75. if (!$headers->has('Message-ID')) {
  76. $headers->addIdHeader('Message-ID', $this->generateMessageId());
  77. }
  78. // remove the Bcc field which should NOT be part of the sent message
  79. $headers->remove('Bcc');
  80. return $headers;
  81. }
  82. public function toString(): string
  83. {
  84. if (null === $body = $this->getBody()) {
  85. $body = new TextPart('');
  86. }
  87. return $this->getPreparedHeaders()->toString().$body->toString();
  88. }
  89. public function toIterable(): iterable
  90. {
  91. if (null === $body = $this->getBody()) {
  92. $body = new TextPart('');
  93. }
  94. yield $this->getPreparedHeaders()->toString();
  95. yield from $body->toIterable();
  96. }
  97. public function ensureValidity()
  98. {
  99. if (!$this->headers->has('To') && !$this->headers->has('Cc') && !$this->headers->has('Bcc')) {
  100. throw new LogicException('An email must have a "To", "Cc", or "Bcc" header.');
  101. }
  102. if (!$this->headers->has('From') && !$this->headers->has('Sender')) {
  103. throw new LogicException('An email must have a "From" or a "Sender" header.');
  104. }
  105. parent::ensureValidity();
  106. }
  107. public function generateMessageId(): string
  108. {
  109. if ($this->headers->has('Sender')) {
  110. $sender = $this->headers->get('Sender')->getAddress();
  111. } elseif ($this->headers->has('From')) {
  112. $sender = $this->headers->get('From')->getAddresses()[0];
  113. } else {
  114. throw new LogicException('An email must have a "From" or a "Sender" header.');
  115. }
  116. return bin2hex(random_bytes(16)).strstr($sender->getAddress(), '@');
  117. }
  118. public function __serialize(): array
  119. {
  120. return [$this->headers, $this->body];
  121. }
  122. public function __unserialize(array $data): void
  123. {
  124. [$this->headers, $this->body] = $data;
  125. }
  126. }