Search code examples
phpsymfonyemailfosuserbundle

Try to use a template with FOSUserBundle for email checking


I use FOSUserBundle to handle my user access management.

The registration form is working fine, my user is registered in my database and the check-email page is web displayed.

The problem is about the confirmation e-mail. It is sent to the user e-mail but it doesn't use the template defined in my app/config.yml.

So I think I didn't define it as it should be.

Actually I only found examples of config.yml where the template parameter is defined in there resetting section, although what I'm interested in isn't the resetting part (not yet) but rather the registration part.

Here is my config.yml file :

fos_user:
db_driver: orm
firewall_name: main
user_class: CSW78\Bundle\Entity\User
service:
    mailer: fos_user.mailer.twig_swift
registration:
    form:
        type: user
    confirmation:
        enabled: false
        from_email:
            address: xxx.xxx@icloud.com
            sender_name: bla bla bla
resetting:
    email:
        template: Mail/registration.email.twig

So my registration.email.twig is located in app/Resources/views/Mail.

By the way I tested Swift Mailer functionality itself through my contact form and it sends email perfectly.


Solution

  • Given:

    fos_user:
         service:
             mailer: fos_user.mailer.twig_swift
         registration:
            form:
                type: user
            confirmation:
                enabled: false
                template: Email/registration.email.twig
                from_email:
                    address: xxx.xxx@icloud.com
                    sender_name: bla bla bla
    

    Then the user e-mail will use the template at app/Resources/views/Email/registration.email.twig.

    Email/registration.email.twig:

    {% block subject %}Registration confirmation{% endblock %}
    
    {% block body_text %}
    {% autoescape false %}
    Hello {{ user.firstName }} {{ user.lastName }} !
    
    Please confirm your registration by visiting {{ confirmationUrl }}
    
    Thank you,
    The Team
    {% endautoescape %}
    {% endblock %}
    
    {% block body_html %}
    {% include 'Email/registration.html.twig' %}
    {% endblock %}
    

    Email/registration.html.twig:

    {% block body_html %}
        <p>Hello <b>{{ user.firstName }} {{ user.lastName }}</b>!</p>
    
        <p>Please confirm by visiting <a href="{{ confirmationUrl }}">this link</a>.</p>
    
        <p>Thank you.</p>
    
        <p>The Team</p>
    {% endblock %}