You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.1 KiB

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