Override template email FOSUserBundle

2019-08-18 11:54发布

问题:

https://symfony.com/doc/current/bundles/FOSUserBundle/emails.html

This link explains apparently how to override the email template from FOSUserBundle for resetting password for a user.

I got a new file for the Resetting email (before was @FOSUser/Resetting/email.txt.twig) and now throw the config.yml I can tell FOSUserBundle to use another file.

fos_user:
    service:
        mailer: fos_user.mailer.twig_swift
    resetting:
        email:
            template:   'email/password_resetting.email.twig'

In the link says that if I add "mailer: fos_user.mailer.twig_swift" will be possible to handle html code.

This new file I need to add a HTML code, so I tried as it says in the documentation:

Adding all the html code inside {% block body_html %} with or without "autoescape" -> same result... i can see all the html tags...

What I'm doing wrong?

Ex:

{# app/Resources/views/email/password_resetting.email.twig #}

{% block subject %}Resetting your password{% endblock %}

{% block body_text %}
    {% autoescape false %}
        Hello {{ user.username }} !

        You can reset your password by accessing {{ confirmationUrl }}

        Greetings,
        the App team
    {% endautoescape %}
{% endblock %}

{% block body_html %}
    {#
        //You can of course render the html directly here.
        //Including a template as done here allows keeping things DRY by using
        //the template inheritance in it
    #}
    <p><b>Test</b> test test</p>
    {{ '<p><b>Test</b> test test</p>'|raw }}

    {% include 'email/password_resetting.html.twig' %}
{% endblock %}

And the content from email/pasword_resetting.html.twig is:

<p><b>Test</b> test test</p>
{{ '<p><b>Test</b> test test</p>'|raw }}

And I get:

  Hello ricard !

    You can reset your password by accessing https://blablabla.bla/app_dev.php/es/resetting/reset/MiPqznsUxHQLLgviDYtCsJrQZBiaqVzDU5ENvHcadA

    Greetings,
    the App team

        <p><b>Test</b> test test</p>
    <p><b>Test</b> test test</p>
    <p><b>Test</b> test test</p>
<p><b>Test</b> test test</p>

I would like to see the bold and formated by paragraph sentence not the tags obviously.

I tried also:

{# app/Resources/views/email/password_resetting.email.twig #}

{% block subject %}Resetting your password{% endblock %}

{% block body_text %}
    {% autoescape false %}
        Hello {{ user.username }} !

        You can reset your password by accessing {{ confirmationUrl }}

        Greetings,
        the App team
    {% endautoescape %}
{% endblock %}

{% block body_html %}
    {#
        //You can of course render the html directly here.
        //Including a template as done here allows keeping things DRY by using
        //the template inheritance in it
    #}
    {% autoescape 'html' %}
        <p><b>Test</b> test test</p>
        {{ '<p><b>Test</b> test test</p>'|raw }}
    {% endautoescape %}
    {% autoescape %}
        <p><b>Test</b> test test</p>
        {{ '<p><b>Test</b> test test</p>'|raw }}
    {% endautoescape %}
    <p><b>Test</b> test test</p>
    {{ '<p><b>Test</b> test test</p>'|raw }}

{% endblock %}

And I get:

Hello ricard !

    You can reset your password by accessing https://blablabla.bla/app_dev.php/es/resetting/reset/2G2ZGW262Z1THu1_80k2vAQMdI4-faNFVFWgdOVts8

    Greetings,
    the App team

            <p><b>Test</b> test test</p>
    <p><b>Test</b> test test</p>
            <p><b>Test</b> test test</p>
    <p><b>Test</b> test test</p>
    <p><b>Test</b> test test</p>
<p><b>Test</b> test test</p>

回答1:

Oks, finally I could find the answer.

I was rewriting the ResettingController and in the services.yml was declared the dependency injection to this new Controller I wanted to use instead of the default controller from FOSUserBundle.

So I got:

AppBundle\Controller\ResettingController:
    class: AppBundle\Controller\ResettingController
    arguments: ['','@fos_user.resetting.form.factory', '','','@fos_user.mailer.default', '']

Replaced by:

AppBundle\Controller\ResettingController:
    class: AppBundle\Controller\ResettingController
    arguments: ['','@fos_user.resetting.form.factory', '','','@fos_user.mailer.twig_swift', '']