Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,4 +39,18 @@ public function indexAction(MailerInterface $mailer)

return new Response();
}

public function sendTemplatedEmail(MailerInterface $mailer)
{
$mail = (new TemplatedEmail())
->to('[email protected]')->from('[email protected]')->subject('Foo')
->addReplyTo('[email protected]')
->htmlTemplate('mail.html.twig')
->context(['foo' => 'bar'])
;

$mailer->send($mail);

return new Response();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
28 changes: 28 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,32 @@ public function testMailerAssertions()
$this->assertEmailAddressContains($email, 'To', '[email protected]');
$this->assertEmailAddressContains($email, 'Reply-To', '[email protected]');
}

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', '[email protected]');
$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', '[email protected]');
$this->assertEmailTextBodyContains($email, 'Test foo is bar!');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
imports:
- { resource: ../config/default.yml }
- { resource: services.yml }

framework:
mailer:
dsn: 'null://null'
envelope:
sender: [email protected]
recipients:
- [email protected]
profiler: ~

messenger:
transports:
default: 'logger://'

routing:
Symfony\Component\Mailer\Messenger\SendEmailMessage: default

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test foo is {{ foo }}!
8 changes: 5 additions & 3 deletions src/Symfony/Component/Mailer/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down