Skip to content

[Mailer] Add html_to_text_converter config docs #17633

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
Dec 30, 2022
Merged
Show file tree
Hide file tree
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
32 changes: 21 additions & 11 deletions mailer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -717,12 +717,22 @@ method of the ``TemplatedEmail`` class and also to a special variable called
Text Content
~~~~~~~~~~~~

When the text content of a ``TemplatedEmail`` is not explicitly defined, mailer
will generate it automatically by converting the HTML contents into text. If you
have `league/html-to-markdown`_ installed in your application,
it uses that to turn HTML into Markdown (so the text email has some visual appeal).
Otherwise, it applies the :phpfunction:`strip_tags` PHP function to the original
HTML contents.
When the text content of a ``TemplatedEmail`` is not explicitly defined, it is
automatically generated from the HTML contents.

Symfony uses the following strategy when generating the text version of an
email:

* If an explicit HTML to text converter has been configured (see
:ref:`twig.mailer.html_to_text_converter
<config-twig-html-to-text-converter>`), it calls it;

* If not, and if you have `league/html-to-markdown`_ installed in your
application, it uses it to turn HTML into Markdown (so the text email has
some visual appeal);

* Otherwise, it applies the :phpfunction:`strip_tags` PHP function to the
original HTML contents.

If you want to define the text content yourself, use the ``text()`` method
explained in the previous sections or the ``textTemplate()`` method provided by
Expand All @@ -732,13 +742,13 @@ the ``TemplatedEmail`` class:

+ use Symfony\Bridge\Twig\Mime\TemplatedEmail;

$email = (new TemplatedEmail())
// ...
$email = (new TemplatedEmail())
// ...

->htmlTemplate('emails/signup.html.twig')
->htmlTemplate('emails/signup.html.twig')
+ ->textTemplate('emails/signup.txt.twig')
// ...
;
// ...
;

.. _mailer-twig-embedding-images:

Expand Down
19 changes: 19 additions & 0 deletions reference/configuration/twig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,25 @@ globals
It defines the global variables injected automatically into all Twig templates.
Learn more about :doc:`Twig global variables </templating/global_variables>`.

mailer
~~~~~~

.. _config-twig-html-to-text-converter:

html_to_text_converter
......................

**type**: ``string`` **default**: ````

.. versionadded:: 6.2

The ``html_to_text_converter`` option was introduced in Symfony 6.2.

The service implementing
:class:`Symfony\\Component\\Mime\\HtmlToTextConverter\\HtmlToTextConverterInterface`
that will be used to automatically create the text part of an email from its
HTML contents when not explicitely defined.

number_format
~~~~~~~~~~~~~

Expand Down