Dashboard sipadu mbip
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.

Message.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * @experimental in 4.3
  19. */
  20. class Message extends RawMessage
  21. {
  22. private $headers;
  23. private $body;
  24. public function __construct(Headers $headers = null, AbstractPart $body = null)
  25. {
  26. $this->headers = $headers ? clone $headers : new Headers();
  27. $this->body = $body;
  28. }
  29. public function __clone()
  30. {
  31. if (null !== $this->headers) {
  32. $this->headers = clone $this->headers;
  33. }
  34. if (null !== $this->body) {
  35. $this->body = clone $this->body;
  36. }
  37. }
  38. /**
  39. * @return $this
  40. */
  41. public function setBody(AbstractPart $body = null)
  42. {
  43. $this->body = $body;
  44. return $this;
  45. }
  46. public function getBody(): ?AbstractPart
  47. {
  48. return $this->body;
  49. }
  50. /**
  51. * @return $this
  52. */
  53. public function setHeaders(Headers $headers)
  54. {
  55. $this->headers = $headers;
  56. return $this;
  57. }
  58. public function getHeaders(): Headers
  59. {
  60. return $this->headers;
  61. }
  62. public function getPreparedHeaders(): Headers
  63. {
  64. $headers = clone $this->headers;
  65. if (!$headers->has('From')) {
  66. throw new LogicException('An email must have a "From" header.');
  67. }
  68. $headers->addTextHeader('MIME-Version', '1.0');
  69. if (!$headers->has('Date')) {
  70. $headers->addDateHeader('Date', new \DateTimeImmutable());
  71. }
  72. // determine the "real" sender
  73. $senders = $headers->get('From')->getAddresses();
  74. $sender = $senders[0];
  75. if ($headers->has('Sender')) {
  76. $sender = $headers->get('Sender')->getAddress();
  77. } elseif (\count($senders) > 1) {
  78. $headers->addMailboxHeader('Sender', $sender);
  79. }
  80. if (!$headers->has('Message-ID')) {
  81. $headers->addIdHeader('Message-ID', $this->generateMessageId($sender->getAddress()));
  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. private function generateMessageId(string $email): string
  103. {
  104. return bin2hex(random_bytes(16)).strstr($email, '@');
  105. }
  106. public function __serialize(): array
  107. {
  108. return [$this->headers, $this->body];
  109. }
  110. public function __unserialize(array $data): void
  111. {
  112. [$this->headers, $this->body] = $data;
  113. }
  114. }