vendor/symfony/mailer/Mailer.php line 62

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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 Psr\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\Mailer\Event\MessageEvent;
  13. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  14. use Symfony\Component\Mailer\Messenger\SendEmailMessage;
  15. use Symfony\Component\Mailer\Transport\TransportInterface;
  16. use Symfony\Component\Messenger\Exception\HandlerFailedException;
  17. use Symfony\Component\Messenger\MessageBusInterface;
  18. use Symfony\Component\Mime\RawMessage;
  19. /**
  20.  * @author Fabien Potencier <[email protected]>
  21.  */
  22. final class Mailer implements MailerInterface
  23. {
  24.     private TransportInterface $transport;
  25.     private ?MessageBusInterface $bus;
  26.     private ?EventDispatcherInterface $dispatcher;
  27.     public function __construct(TransportInterface $transportMessageBusInterface $bus nullEventDispatcherInterface $dispatcher null)
  28.     {
  29.         $this->transport $transport;
  30.         $this->bus $bus;
  31.         $this->dispatcher $dispatcher;
  32.     }
  33.     public function send(RawMessage $messageEnvelope $envelope null): void
  34.     {
  35.         if (null === $this->bus) {
  36.             $this->transport->send($message$envelope);
  37.             return;
  38.         }
  39.         $stamps = [];
  40.         if (null !== $this->dispatcher) {
  41.             // The dispatched event here has `queued` set to `true`; the goal is NOT to render the message, but to let
  42.             // listeners do something before a message is sent to the queue.
  43.             // We are using a cloned message as we still want to dispatch the **original** message, not the one modified by listeners.
  44.             // That's because the listeners will run again when the email is sent via Messenger by the transport (see `AbstractTransport`).
  45.             // Listeners should act depending on the `$queued` argument of the `MessageEvent` instance.
  46.             $clonedMessage = clone $message;
  47.             $clonedEnvelope null !== $envelope ? clone $envelope Envelope::create($clonedMessage);
  48.             $event = new MessageEvent($clonedMessage$clonedEnvelope, (string) $this->transporttrue);
  49.             $this->dispatcher->dispatch($event);
  50.             $stamps $event->getStamps();
  51.         }
  52.         try {
  53.             $this->bus->dispatch(new SendEmailMessage($message$envelope), $stamps);
  54.         } catch (HandlerFailedException $e) {
  55.             foreach ($e->getNestedExceptions() as $nested) {
  56.                 if ($nested instanceof TransportExceptionInterface) {
  57.                     throw $nested;
  58.                 }
  59.             }
  60.             throw $e;
  61.         }
  62.     }
  63. }