Message.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. throw new LogicException('An email must have a "From" header.');
  63. }
  64. $headers->addTextHeader('MIME-Version', '1.0');
  65. if (!$headers->has('Date')) {
  66. $headers->addDateHeader('Date', new \DateTimeImmutable());
  67. }
  68. // determine the "real" sender
  69. if (!$headers->has('Sender') && \count($froms = $headers->get('From')->getAddresses()) > 1) {
  70. $headers->addMailboxHeader('Sender', $froms[0]);
  71. }
  72. if (!$headers->has('Message-ID')) {
  73. $headers->addIdHeader('Message-ID', $this->generateMessageId());
  74. }
  75. // remove the Bcc field which should NOT be part of the sent message
  76. $headers->remove('Bcc');
  77. return $headers;
  78. }
  79. public function toString(): string
  80. {
  81. if (null === $body = $this->getBody()) {
  82. $body = new TextPart('');
  83. }
  84. return $this->getPreparedHeaders()->toString().$body->toString();
  85. }
  86. public function toIterable(): iterable
  87. {
  88. if (null === $body = $this->getBody()) {
  89. $body = new TextPart('');
  90. }
  91. yield $this->getPreparedHeaders()->toString();
  92. yield from $body->toIterable();
  93. }
  94. public function ensureValidity()
  95. {
  96. if (!$this->headers->has('From')) {
  97. throw new LogicException('An email must have a "From" header.');
  98. }
  99. parent::ensureValidity();
  100. }
  101. public function generateMessageId(): string
  102. {
  103. if ($this->headers->has('Sender')) {
  104. $sender = $this->headers->get('Sender')->getAddress();
  105. } elseif ($this->headers->has('From')) {
  106. $sender = $this->headers->get('From')->getAddresses()[0];
  107. } else {
  108. throw new LogicException('An email must have a "From" or a "Sender" header to compute a Messsage ID.');
  109. }
  110. return bin2hex(random_bytes(16)).strstr($sender->getAddress(), '@');
  111. }
  112. public function __serialize(): array
  113. {
  114. return [$this->headers, $this->body];
  115. }
  116. public function __unserialize(array $data): void
  117. {
  118. [$this->headers, $this->body] = $data;
  119. }
  120. }