覆盖模板电子邮件 FOSUserBundle

Override template email FOSUserBundle

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

此 link 显然解释了如何覆盖 FOSUserBundle 中的电子邮件模板以重置用户密码。

我得到了一个用于重置电子邮件的新文件(之前是 @FOSUser/Resetting/email.txt.twig),现在抛出 config.yml 我可以告诉 FOSUserBundle 使用另一个文件。

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

在link中说如果我加上"mailer: fos_user.mailer.twig_swift"就可以处理html代码。

这个新文件我需要添加一个HTML代码,所以我按照文档中的说法进行了尝试:

在 {% block body_html %} 中添加所有 html 代码,有或没有 "autoescape" -> 相同的结果...我可以看到所有 html标签...

我做错了什么?

例如:

{# 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 %}

来自email/pasword_resetting.html.twig的内容是:

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

我得到:

  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>

我希望看到的是粗体和按段落格式设置的句子,而不是标签。

我也试过:

{# 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 %}

我得到:

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>

好的,终于找到答案了

我正在重写 ResettingController 并在 services.yml 中声明了对我想使用的这个新控制器的依赖注入,而不是来自 FOSUserBundle 的默认控制器。

所以我得到了:

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

替换为:

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