8000 minor #17065 [Mailer] Add more information about sending email async … · symfony/symfony-docs@b15763c · GitHub
[go: up one dir, main page]

Skip to content

Commit b15763c

Browse files
committed
minor #17065 [Mailer] Add more information about sending email async (fabpot)
This PR was merged into the 6.2 branch. Discussion ---------- [Mailer] Add more information about sending email async The example only works as of 6.2 thanks to symfony/symfony#47075 I've written this PR because we keep having issues like symfony/symfony#44439. So, documenting a bit more how it works internally might help people understand what to do. Commits ------- 4fbbc16 Add more information about sending email async
2 parents d93617a + 4fbbc16 commit b15763c

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

mailer.rst

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,9 +1204,38 @@ you have a transport called ``async``, you can route the message there:
12041204
->senders(['async']);
12051205
};
12061206
1207+
Thanks to this, instead of being delivered immediately, messages will be sent
1208+
to the transport to be handled later (see :ref:`messenger-worker`). Note that
1209+
the "rendering" of the email (computed headers, body rendering, ...) is also
1210+
deferred and will only happen just before the email is sent by the Messenger
1211+
handler.
12071212

1208-
Thanks to this, instead of being delivered immediately, messages will be sent to
1209-
the transport to be handled later (see :ref:`messenger-worker`).
1213+
.. versionadded:: 6.2
1214+
1215+
The following example about rendering the email before calling
1216+
``$mailer->send($email)`` works as of Symfony 6.2.
1217+
1218+
When sending an email asynchronously, its instance must be serializable.
1219+
This is always the case for :class:`Symfony\\Bridge\\Twig\\Mime\\Email`
1220+
instances, but when sending a
1221+
:class:`Symfony\\Bridge\\Twig\\Mime\\TemplatedEmail`, you must ensure that
1222+
the ``context`` is serializable. If you have non-serializable variables,
1223+
like Doctrine entities, either replace them with more specific variables or
1224+
render the email before calling ``$mailer->send($email)``::
1225+
1226+
use Symfony\Component\Mailer\MailerInterface;
1227+
use Symfony\Component\Mime\BodyRendererInterface;
1228+
1229+
public function action(MailerInterface $mailer, BodyRendererInterface $bodyRenderer)
1230+
{
1231+
$email = (new TemplatedEmail())
1232+
->htmlTemplate($template)
1233+
->context($context)
1234+
;
1235+
$bodyRenderer->render($email);
1236+
1237+
$mailer->send($email);
1238+
}
12101239

12111240
You can configure which bus is used to dispatch the message using the ``message_bus`` option.
12121241
You can also set this to ``false`` to call the Mailer transport directly and

0 commit comments

Comments
 (0)
0