Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita39cd4f

Browse files
committed
Add more information about sending email async
1 parentd93617a commita39cd4f

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

‎mailer.rst‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,9 +1204,40 @@ 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+
..code-block::php
1227+
1228+
use Symfony\Component\Mailer\MailerInterface;
1229+
use Symfony\Component\Mime\BodyRendererInterface;
1230+
1231+
public function action(MailerInterface $mailer, BodyRendererInterface $bodyRenderer)
1232+
{
1233+
$email = (new TemplatedEmail())
1234+
->htmlTemplate($template)
1235+
->context($context)
1236+
;
1237+
$bodyRenderer->render($email);
1238+
1239+
$mailer->send($email);
1240+
}
12101241
12111242
You can configure which bus is used to dispatch the message using the ``message_bus`` option.
12121243
You can also set this to ``false`` to call the Mailer transport directly and

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp