diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/EmailController.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/EmailController.php index 1a871f79be907..d0adad69c573c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/EmailController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/EmailController.php @@ -11,6 +11,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller; +use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mime\Address; @@ -38,4 +39,18 @@ public function indexAction(MailerInterface $mailer) return new Response(); } + + public function sendTemplatedEmail(MailerInterface $mailer) + { + $mail = (new TemplatedEmail()) + ->to('fabien@symfony.com')->from('fabien@symfony.com')->subject('Foo') + ->addReplyTo('me@symfony.com') + ->htmlTemplate('mail.html.twig') + ->context(['foo' => 'bar']) + ; + + $mailer->send($mail); + + return new Response(); + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml index 155871fc278ec..fa5fd78109068 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml @@ -60,3 +60,7 @@ array_controller: send_email: path: /send_email defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\EmailController::indexAction } + +send_templated_email: + path: /send_templated_email + defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\EmailController::sendTemplatedEmail } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php index ec293315cafaa..1aadc8b29f6ad 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php @@ -99,4 +99,32 @@ public function testMailerAssertions() $this->assertEmailAddressContains($email, 'To', 'thomas@symfony.com'); $this->assertEmailAddressContains($email, 'Reply-To', 'me@symfony.com'); } + + public function testSendingTemplatedEmail() + { + $client = $this->createClient(['test_case' => 'Mailer', 'root_config' => 'config.yml', 'debug' => true]); + $client->request('GET', '/send_templated_email'); + $this->assertResponseIsSuccessful(); + + $this->assertEmailCount(1); + + $email = $this->getMailerMessage(); + $this->assertEmailHasHeader($email, 'To'); + $this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com'); + $this->assertEmailTextBodyContains($email, 'Test foo is bar!'); + } + + public function testSendingQueuedTemplatedEmail() + { + $client = $this->createClient(['test_case' => 'Mailer', 'root_config' => 'config_messenger.yml', 'debug' => true]); + $client->request('GET', '/send_templated_email'); + $this->assertResponseIsSuccessful(); + + $this->assertEmailCount(1); + + $email = $this->getMailerMessage(); + $this->assertEmailHasHeader($email, 'To'); + $this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com'); + $this->assertEmailTextBodyContains($email, 'Test foo is bar!'); + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/bundles.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/bundles.php index 15ff182c6fed5..50676b63646eb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/bundles.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/bundles.php @@ -11,8 +11,10 @@ use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle; +use Symfony\Bundle\TwigBundle\TwigBundle; return [ new FrameworkBundle(), + new TwigBundle(), new TestBundle(), ]; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/config_messenger.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/config_messenger.yml new file mode 100644 index 0000000000000..cf5196e173fca --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/config_messenger.yml @@ -0,0 +1,20 @@ +imports: + - { resource: ../config/default.yml } + - { resource: services.yml } + +framework: + mailer: + dsn: 'null://null' + envelope: + sender: sender@example.org + recipients: + - redirected@example.org + profiler: ~ + + messenger: + transports: + default: 'logger://' + + routing: + Symfony\Component\Mailer\Messenger\SendEmailMessage: default + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/templates/mail.html.twig b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/templates/mail.html.twig new file mode 100644 index 0000000000000..a5767f4b6ccae --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/templates/mail.html.twig @@ -0,0 +1 @@ +Test foo is {{ foo }}! diff --git a/src/Symfony/Component/Mailer/Mailer.php b/src/Symfony/Component/Mailer/Mailer.php index d15aa558f7075..c7dafc23c640a 100644 --- a/src/Symfony/Component/Mailer/Mailer.php +++ b/src/Symfony/Component/Mailer/Mailer.php @@ -38,16 +38,18 @@ public function __construct(TransportInterface $transport, MessageBusInterface $ public function send(RawMessage $message, Envelope $envelope = null): void { + // If a bus is not available, send directly to the transport if (null === $this->bus) { $this->transport->send($message, $envelope); return; } + // Allows the transformation of a Message and the Envelope before the email is sent if (null !== $this->dispatcher) { - $clonedMessage = clone $message; - $clonedEnvelope = null !== $envelope ? clone $envelope : Envelope::create($clonedMessage); - $event = new MessageEvent($clonedMessage, $clonedEnvelope, (string) $this->transport, true); + $envelope = null !== $envelope ? $envelope : Envelope::create($message); + $event = new MessageEvent($message, $envelope, (string) $this->transport, true); + $this->dispatcher->dispatch($event); }