EmailMessage.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Notifier\Message;
  11. use Symfony\Bridge\Twig\Mime\NotificationEmail;
  12. use Symfony\Component\Mailer\Envelope;
  13. use Symfony\Component\Mime\Email;
  14. use Symfony\Component\Mime\RawMessage;
  15. use Symfony\Component\Notifier\Exception\LogicException;
  16. use Symfony\Component\Notifier\Notification\Notification;
  17. use Symfony\Component\Notifier\Recipient\Recipient;
  18. /**
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. *
  21. * @experimental in 5.1
  22. */
  23. final class EmailMessage implements MessageInterface
  24. {
  25. private $message;
  26. private $envelope;
  27. public function __construct(RawMessage $message, Envelope $envelope = null)
  28. {
  29. $this->message = $message;
  30. $this->envelope = $envelope;
  31. }
  32. public static function fromNotification(Notification $notification, Recipient $recipient): self
  33. {
  34. if (!class_exists(NotificationEmail::class)) {
  35. $email = (new Email())
  36. ->to($recipient->getEmail())
  37. ->subject($notification->getSubject())
  38. ->text($notification->getContent() ?: $notification->getSubject())
  39. ;
  40. } else {
  41. $email = (new NotificationEmail())
  42. ->to($recipient->getEmail())
  43. ->subject($notification->getSubject())
  44. ->content($notification->getContent() ?: $notification->getSubject())
  45. ->importance($notification->getImportance())
  46. ;
  47. if ($exception = $notification->getException()) {
  48. $email->exception($exception);
  49. }
  50. }
  51. return new self($email);
  52. }
  53. public function getMessage(): RawMessage
  54. {
  55. return $this->message;
  56. }
  57. public function getEnvelope(): ?Envelope
  58. {
  59. return $this->envelope;
  60. }
  61. /**
  62. * @return $this
  63. */
  64. public function envelope(Envelope $envelope): self
  65. {
  66. $this->envelope = $envelope;
  67. return $this;
  68. }
  69. public function getSubject(): string
  70. {
  71. return '';
  72. }
  73. public function getRecipientId(): ?string
  74. {
  75. return null;
  76. }
  77. public function getOptions(): ?MessageOptionsInterface
  78. {
  79. return null;
  80. }
  81. /**
  82. * @return $this
  83. */
  84. public function transport(string $transport): self
  85. {
  86. if (!$this->message instanceof Email) {
  87. throw new LogicException('Cannot set a Transport on a RawMessage instance.');
  88. }
  89. $this->message->getHeaders()->addTextHeader('X-Transport', $transport);
  90. return $this;
  91. }
  92. public function getTransport(): ?string
  93. {
  94. return $this->message instanceof Email ? $this->message->getHeaders()->getHeaderBody('X-Transport') : null;
  95. }
  96. }