10BC0 [Messenger] Templated email messages fails when sending async (thru messenger transport) by ronnylt · Pull Request #39458 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fix templated email sending by not cloning message
  • Loading branch information
ronnylt committed Dec 12, 2020
commit 3c3c39f441ae7eb3572be42b5e3b045ad84e3794
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ 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');
->htmlTemplate('mail.html.twig')
->context(['foo' => 'bar'])
;

$mailer->send($mail);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,31 @@ public function testMailerAssertions()
$this->assertEmailAddressContains($email, 'Reply-To', 'me@symfony.com');
}

public function testSendingQueuedMessage()
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!');
}
}
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 @@
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
0