vendor/symfony/mailer/Mailer.php line 37

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\Mailer;
  11. use Psr\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\Mailer\Event\MessageEvent;
  13. use Symfony\Component\Mailer\Messenger\SendEmailMessage;
  14. use Symfony\Component\Mailer\Transport\TransportInterface;
  15. use Symfony\Component\Messenger\MessageBusInterface;
  16. use Symfony\Component\Mime\RawMessage;
  17. /**
  18.  * @author Fabien Potencier <fabien@symfony.com>
  19.  */
  20. final class Mailer implements MailerInterface
  21. {
  22.     private $transport;
  23.     private $bus;
  24.     private $dispatcher;
  25.     public function __construct(TransportInterface $transport, MessageBusInterface $bus = null, EventDispatcherInterface $dispatcher = null)
  26.     {
  27.         $this->transport = $transport;
  28.         $this->bus = $bus;
  29.         $this->dispatcher = $dispatcher;
  30.     }
  31.     public function send(RawMessage $message, Envelope $envelope = null): void
  32.     {
  33.         if (null === $this->bus) {
  34.             $this->transport->send($message, $envelope);
  35.             return;
  36.         }
  37.         if (null !== $this->dispatcher) {
  38.             $clonedMessage = clone $message;
  39.             $clonedEnvelope = null !== $envelope ? clone $envelope : Envelope::create($clonedMessage);
  40.             $event = new MessageEvent($clonedMessage, $clonedEnvelope, (string) $this->transport, true);
  41.             $this->dispatcher->dispatch($event);
  42.         }
  43.         $this->bus->dispatch(new SendEmailMessage($message, $envelope));
  44.     }
  45. }