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

Appearance settings

[Mailer] Add more information about sending email async #17065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2022
Merged
Changes from all commits
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
33 changes: 31 additions & 2 deletions 33 mailer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1204,9 +1204,38 @@ you have a transport called ``async``, you can route the message there:
->senders(['async']);
};

Thanks to this, instead of being delivered immediately, messages will be sent
to the transport to be handled later (see :ref:`messenger-worker`). Note that
the "rendering" of the email (computed headers, body rendering, ...) is also
deferred and will only happen just before the email is sent by the Messenger
handler.

Thanks to this, instead of being delivered immediately, messages will be sent to
the transport to be handled later (see :ref:`messenger-worker`).
.. versionadded:: 6.2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imho this should be a warning, otherwise it gets removed in Symfony 7 docs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having this removed in 7.0 is a good thing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done then


The following example about rendering the email before calling
``$mailer->send($email)`` works as of Symfony 6.2.

When sending an email asynchronously, its instance must be serializable.
This is always the case for :class:`Symfony\\Bridge\\Twig\\Mime\\Email`
instances, but when sending a
:class:`Symfony\\Bridge\\Twig\\Mime\\TemplatedEmail`, you must ensure that
the ``context`` is serializable. If you have non-serializable variables,
like Doctrine entities, either replace them with more specific variables or
render the email before calling ``$mailer->send($email)``::

use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\BodyRendererInterface;

public function action(MailerInterface $mailer, BodyRendererInterface $bodyRenderer)
{
$email = (new TemplatedEmail())
->htmlTemplate($template)
->context($context)
;
$bodyRenderer->render($email);

$mailer->send($email);
}

You can configure which bus is used to dispatch the message using the ``message_bus`` option.
You can also set this to ``false`` to call the Mailer transport directly and
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.