EmailChannel.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\Channel;
  11. use Symfony\Component\Mailer\Envelope;
  12. use Symfony\Component\Mailer\Messenger\SendEmailMessage;
  13. use Symfony\Component\Mailer\Transport\TransportInterface;
  14. use Symfony\Component\Messenger\MessageBusInterface;
  15. use Symfony\Component\Mime\Email;
  16. use Symfony\Component\Notifier\Exception\LogicException;
  17. use Symfony\Component\Notifier\Message\EmailMessage;
  18. use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
  19. use Symfony\Component\Notifier\Notification\Notification;
  20. use Symfony\Component\Notifier\Recipient\Recipient;
  21. /**
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. *
  24. * @experimental in 5.1
  25. */
  26. class EmailChannel implements ChannelInterface
  27. {
  28. private $transport;
  29. private $bus;
  30. private $from;
  31. private $envelope;
  32. public function __construct(TransportInterface $transport = null, MessageBusInterface $bus = null, string $from = null, Envelope $envelope = null)
  33. {
  34. if (null === $transport && null === $bus) {
  35. throw new LogicException(sprintf('"%s" needs a Transport or a Bus but both cannot be "null".', static::class));
  36. }
  37. $this->transport = $transport;
  38. $this->bus = $bus;
  39. $this->from = $from ?: ($envelope ? $envelope->getSender() : null);
  40. $this->envelope = $envelope;
  41. }
  42. public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void
  43. {
  44. $message = null;
  45. if ($notification instanceof EmailNotificationInterface) {
  46. $message = $notification->asEmailMessage($recipient, $transportName);
  47. }
  48. $message = $message ?: EmailMessage::fromNotification($notification, $recipient, $transportName);
  49. $email = $message->getMessage();
  50. if ($email instanceof Email) {
  51. if (!$email->getFrom()) {
  52. if (null === $this->from) {
  53. throw new LogicException(sprintf('To send the "%s" notification by email, you should either configure a global "from" or set it in the "asEmailMessage()" method.', get_debug_type($notification)));
  54. }
  55. $email->from($this->from);
  56. }
  57. if (!$email->getTo()) {
  58. $email->to($recipient->getEmail());
  59. }
  60. }
  61. if (null !== $this->envelope) {
  62. $message->envelope($this->envelope);
  63. }
  64. if (null !== $transportName) {
  65. $message->transport($transportName);
  66. }
  67. if (null === $this->bus) {
  68. $this->transport->send($message->getMessage(), $message->getEnvelope());
  69. } else {
  70. $this->bus->dispatch(new SendEmailMessage($message->getMessage(), $message->getEnvelope()));
  71. }
  72. }
  73. public function supports(Notification $notification, Recipient $recipient): bool
  74. {
  75. return '' !== $recipient->getEmail();
  76. }
  77. }