@@ -1204,9 +1204,38 @@ you have a transport called ``async``, you can route the message there:
1204
1204
->senders(['async']);
1205
1205
};
1206
1206
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.
1207
1212
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
+ }
1210
1239
1211
1240
You can configure which bus is used to dispatch the message using the ``message_bus `` option.
1212
1241
You can also set this to ``false `` to call the Mailer transport directly and
0 commit comments