0
votes

I'm trying to understand the FOSUserBundle implementation of Twig and a relative novice with Symfony2. I'd like to override the default implementation and send html emails using the fos_user.mailer.twig_swift mailer service.

My environment: Symfony 2.4.1

    "require": {...
    "symfony/swiftmailer-bundle": "~2.3",
     "friendsofsymfony/user-bundle": "*"

Here's where I'm stuck: based on the FOSUserBundle documentation (here). The line that kills me is:

{% include 'AcmeDemoBundle:User:resetting_email.html.twig' %}

Following their instructions I get:

 " Unable to find template "PP2UserBundle:User:resetting_email.html.twig"  - (PP2UserBundle is my implementation of AcmeDemoBundle)

And that error almost makes sense to me. I don't understand where it would find that template. So to keep it from crashing, I delete the include and add some html code to test if it sees my template and sends the email. It does and I get a nice email with "Hi" in the {% block body_html %}. The {% block subject %} also renders. But the {% block body_text %} is empty. Oof.

Here's my code. If anyone could share some insight, I'd really appreciate it!

Service is Registered and looks OK. (Though I have my doubts about the yml implementation) I picked the up xml->yml translation off another post.

services.yml

 fos_user.mailer.twig_swift:
    class: FOS\UserBundle\Mailer\TwigSwiftMailer
    arguments:
        - @mailer
        - @router
        - @twig'
        - { template: { confirmation: %fos_user.registration.confirmation.template%, resetting: %fos_user.resetting.email.template% }, from_email: { confirmation: %fos_user.registration.confirmation.from_email%, resetting: %fos_user.resetting.email.from_email% } }

Config.yml looks logical.

   service:
        mailer: fos_user.mailer.twig_swift
resetting:
        email:
            template: PP2UserBundle:User:resetting.email.twig

And my template placed in Resources/views/User

resetting.email.twig

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

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

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

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

{% block body_html %}
<h1> hi </h1>
{% include 'PP2UserBundle:User:resetting_email.html.twig' %}
{% endblock %}

Again if I take out {% include 'PP2UserBundle:User:resetting_email.html.twig' %} I get a nice little 'hi' but little else.

It feels like I should be extending the fosuser templates in views/Resetting, (or including them) but I don't see which. I'm missing something.

Thanks again.

1
PP2UserBundle:User:resetting.email.html.twig means the template should be in YourBundleFolder/Resources/views/User/resetting.email.html.twig - JamesHalsall

1 Answers

0
votes

You can use your own email template by using twigs embed tag.

For example you have a template in Acme:Email:email.html.twig:

{# src/Acme/DemoBundle/Resources/views/Email/email.html.twig #}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <table>
        <tr>
            <td>
                {% block body_text %}{% endblock %}
            </td>
        </tr>
    </table>
</body>
</html>

Then you can embed this template file in your resetting.email.twig and insert the body_text block:

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

{% block body_html %}
    {% embed "AcmeDemoBundle:Email:email.html.twig" %}
        {% block body_text %}
            {% autoescape false %}
            Hello {{ user.username }} !

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

            Greetings,
            the Acme team
            {% endautoescape %}
        {% endblock %}
    {% endembed %}
{% endblock %}