Email confirmation in symfony.

FOSUserBundle Emails

The FOSUserBundle has built-in support for sending emails in two different instances.

Registration Confirmation

The first is when a new user registers and the bundle is configured to require email confirmation before the user registration is complete. The email that is sent to the new user contains a link that, when visited, will verify the registration and enable the user account.

Requiring email confirmation for a new account is turned off by default. To enable it, update your configuration as follows:

# app/config/config.yml
fos_user:
    # ...
    registration:
        confirmation:
            enabled: true

Configuring the Sender Email Address:

The FOSUserBundle default mailer allows you to configure the sender email address of the emails sent out by the bundle. You can configure the address globally or on a per email basis.

To configure the sender email address for all emails sent out by the bundle, simply update your fos_user config as follows:

# app/config/config.yml
fos_user:
    #...
    from_email:
        address:        noreply@example.com
        sender_name:    Demo App

The bundle also provides the flexibility of allowing you to configure the sender email address for the emails individually.

To configure the sender email address for the user registration confirmation email update your fos_user config as follows:

# app/config/config.yml
fos_user:
    #...
    registration:
        confirmation:
            from_email:
                address:        registration@example.com
                sender_name:    Demo Registration


Sending HTML mails

The default mailer only supports sending plain text messages. If you want to send multipart messages, the easiest solution is to use the TwigSwiftMailer implementation instead. It expects your twig template to define 3 blocks:

  • subject containing the email subject
  • body_text rendering the plain text version of the message
  • body_html rendering the html mail

Here is how you can use it, you can use either of the two methods of referencing the email template below.

# app/config/config.yml
fos_user:
    # ...
    service:
        mailer: fos_user.mailer.twig_swift
    resetting:
        email:
            template:   email/password_resetting.email.twig
    registration:
        confirmation:
            template:   FOSUserBundle:Registration:email.txt.twig


{# 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
#}
{% include 'email/password_resetting.html.twig' %}
{% endblock %}

You can view the default email templates at FOSUserBundle:Registration:email.txt.twig andFOSUserBundle:Resetting:email.txt.twig