Mailer.php 2.9 KB

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