SentMessage.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Mailer;
  11. use Symfony\Component\Mime\Message;
  12. use Symfony\Component\Mime\RawMessage;
  13. /**
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class SentMessage
  17. {
  18. private RawMessage $original;
  19. private RawMessage $raw;
  20. private string $messageId;
  21. private string $debug = '';
  22. /**
  23. * @internal
  24. */
  25. public function __construct(
  26. RawMessage $message,
  27. private Envelope $envelope,
  28. ) {
  29. $message->ensureValidity();
  30. $this->original = $message;
  31. if ($message instanceof Message) {
  32. $message = clone $message;
  33. $headers = $message->getHeaders();
  34. if (!$headers->has('Message-ID')) {
  35. $headers->addIdHeader('Message-ID', $message->generateMessageId());
  36. }
  37. $this->messageId = $headers->get('Message-ID')->getId();
  38. $this->raw = new RawMessage($message->toIterable());
  39. } else {
  40. $this->raw = $message;
  41. }
  42. }
  43. public function getMessage(): RawMessage
  44. {
  45. return $this->raw;
  46. }
  47. public function getOriginalMessage(): RawMessage
  48. {
  49. return $this->original;
  50. }
  51. public function getEnvelope(): Envelope
  52. {
  53. return $this->envelope;
  54. }
  55. /**
  56. * Sets the transport-level message ID.
  57. */
  58. public function setMessageId(string $id): void
  59. {
  60. $this->messageId = $id;
  61. }
  62. /**
  63. * Gets the transport-level message ID.
  64. *
  65. * Not to be confused with the Message-ID header, which is available via getOriginalMessage()
  66. */
  67. public function getMessageId(): string
  68. {
  69. return $this->messageId;
  70. }
  71. public function getDebug(): string
  72. {
  73. return $this->debug;
  74. }
  75. public function appendDebug(string $debug): void
  76. {
  77. $this->debug .= $debug;
  78. }
  79. public function toString(): string
  80. {
  81. return $this->raw->toString();
  82. }
  83. public function toIterable(): iterable
  84. {
  85. return $this->raw->toIterable();
  86. }
  87. }