diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 8a149e25515e5..0d1c6acf11860 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -1556,6 +1556,7 @@ private function addMailerSection(ArrayNodeDefinition $rootNode) ->{!class_exists(FullStack::class) && class_exists(Mailer::class) ? 'canBeDisabled' : 'canBeEnabled'}() ->children() ->scalarNode('dsn')->defaultValue('smtp://null')->end() + ->booleanNode('async')->defaultValue(false)->end() ->arrayNode('envelope') ->info('Mailer Envelope configuration') ->children() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 7ed04a5bb15b7..ae37a98c020b4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1952,6 +1952,8 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co } $loader->load('mailer.xml'); + + $container->getDefinition('mailer.mailer')->setArgument(2, $config['async']); $container->getDefinition('mailer.default_transport')->setArgument(0, $config['dsn']); $recipients = $config['envelope']['recipients'] ?? null; diff --git a/src/Symfony/Component/Mailer/Mailer.php b/src/Symfony/Component/Mailer/Mailer.php index 324f50cad7803..034f534417a60 100644 --- a/src/Symfony/Component/Mailer/Mailer.php +++ b/src/Symfony/Component/Mailer/Mailer.php @@ -23,16 +23,22 @@ class Mailer implements MailerInterface { private $transport; private $bus; + private $async; - public function __construct(TransportInterface $transport, MessageBusInterface $bus = null) + public function __construct(TransportInterface $transport, MessageBusInterface $bus = null, bool $async = false) { + if ($async && null === $bus) { + throw new \LogicException('You cannot send messages asynchronously as the Symfony Messenger Component is not installed. Try running "composer require symfony/messenger".'); + } + $this->transport = $transport; $this->bus = $bus; + $this->async = $async; } public function send(RawMessage $message, SmtpEnvelope $envelope = null): void { - if (null === $this->bus) { + if (!$this->async) { $this->transport->send($message, $envelope); return;