vendor/symfony/notifier/Channel/EmailChannel.php line 36

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