8000 Fix templated email sending by not cloning message · symfony/symfony@859598c · GitHub
[go: up one dir, main page]

Skip to content

Commit 859598c

Browse files
committed
Fix templated email sending by not cloning message
1 parent fa6e8e7 commit 859598c

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/EmailController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public function sendTemplatedEmail(MailerInterface $mailer)
4545
$mail = (new TemplatedEmail())
4646
->to('fabien@symfony.com')->from('fabien@symfony.com')->subject('Foo')
4747
->addReplyTo('me@symfony.com')
48-
->htmlTemplate('mail.html.twig');
48+
->htmlTemplate('mail.html.twig')
49+
->context(['foo' => 'bar'])
50+
;
4951

5052
$mailer->send($mail);
5153

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/views/mail.html.twig

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,31 @@ public function testMailerAssertions()
100100
$this->assertEmailAddressContains($email, 'Reply-To', 'me@symfony.com');
101101
}
102102

103-
public function testSendingQueuedMessage()
103+
public function testSendingTemplatedEmail()
104+
{
105+
$client = $this->createClient(['test_case' => 'Mailer', 'root_config' => 'config.yml', 'debug' => true]);
106+
$client->request('GET', '/send_templated_email');
107+
$this->assertResponseIsSuccessful();
108+
109+
$this->assertEmailCount(1);
110+
111+
$email = $this->getMailerMessage();
112+
$this->assertEmailHasHeader($email, 'To');
113+
$this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com');
114+
$this->assertEmailTextBodyContains($email, 'Test foo is bar!');
115+
}
116+
117+
public function testSendingQueuedTemplatedEmail()
104118
{
105119
$client = $this->createClient(['test_case' => 'Mailer', 'root_config' => 'config_messenger.yml', 'debug' => true]);
106120
$client->request('GET', '/send_templated_email');
107121
$this->assertResponseIsSuccessful();
122+
123+
$this->assertEmailCount(1);
124+
125+
$email = $this->getMailerMessage();
126+
$this->assertEmailHasHeader($email, 'To');
127+
$this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com');
128+
$this->assertEmailTextBodyContains($email, 'Test foo is bar!');
108129
}
109130
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/bundles.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
1313
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
14+
use Symfony\Bundle\TwigBundle\TwigBundle;
1415

1516
return [
1617
new FrameworkBundle(),
18+
new TwigBundle(),
1719
new TestBundle(),
1820
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test foo is {{ foo }}!

src/Symfony/Component/Mailer/Mailer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ public function __construct(TransportInterface $transport, MessageBusInterface $
3838

3939
public function send(RawMessage $message, Envelope $envelope = null): void
4040
{
41+
// If a bus is not available, send directly to the transport
4142
if (null === $this->bus) {
4243
$this->transport->send($message, $envelope);
4344

4445
return;
4546
}
4647

48+
// Allows the transformation of a Message and the Envelope before the email is sent
4749
if (null !== $this->dispatcher) {
48-
$clonedMessage = clone $message;
49-
$clonedEnvelope = null !== $envelope ? clone $envelope : Envelope::create($clonedMessage);
50-
$event = new MessageEvent($clonedMessage, $clonedEnvelope, (string) $this->transport, true);
50+
$envelope = null !== $envelope ? $envelope : Envelope::create($message);
51+
$event = new MessageEvent($message, $envelope, (string) $this->transport, true);
52+
5153
$this->dispatcher->dispatch($event);
5254
}
5355

0 commit comments

Comments
 (0)
0