emails.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. FOSUserBundle Emails
  2. ====================
  3. The FOSUserBundle has built-in support for sending emails in two different
  4. instances.
  5. Registration Confirmation
  6. -------------------------
  7. The first is when a new user registers and the bundle is configured
  8. to require email confirmation before the user registration is complete.
  9. The email that is sent to the new user contains a link that, when visited,
  10. will verify the registration and enable the user account.
  11. Requiring email confirmation for a new account is turned off by default.
  12. To enable it, update your configuration as follows:
  13. .. code-block:: yaml
  14. # app/config/config.yml
  15. fos_user:
  16. # ...
  17. registration:
  18. confirmation:
  19. enabled: true
  20. Password Reset
  21. --------------
  22. An email is also sent when a user has requested a password reset. The
  23. FOSUserBundle provides password reset functionality in a two-step process.
  24. First the user must request a password reset. After the request has been
  25. made, an email is sent containing a link to visit. Upon visiting the link,
  26. the user will be identified by the token contained in the url. When the user
  27. visits the link and the token is confirmed, the user will be presented with
  28. a form to enter in a new password.
  29. Default Mailer Implementations
  30. ------------------------------
  31. The bundle comes with three mailer implementations. They are listed below
  32. by service id:
  33. - ``fos_user.mailer.default`` is the default implementation, and uses Swiftmailer to send emails.
  34. - ``fos_user.mailer.twig_swift`` uses Swiftmailer to send emails and Twig blocks to render the message.
  35. - ``fos_user.mailer.noop`` is a mailer implementation which performs no operation, so no emails are sent.
  36. .. note::
  37. The ``fos_user.mailer.noop`` mailer service should be used in the case
  38. where you do not want the bundle to send emails and you do not want to
  39. include the SwiftmailerBundle in your app. If you leave the default implementation
  40. configured as the mailer and do not have the SwiftmailerBundle registered,
  41. you will receive an exception because of a missing dependency.
  42. Configuring the Sender Email Address
  43. ------------------------------------
  44. The FOSUserBundle default mailer allows you to configure the sender email address
  45. of the emails sent out by the bundle. You can configure the address globally or on
  46. a per email basis.
  47. To configure the sender email address for all emails sent out by the bundle, simply
  48. update your ``fos_user`` config as follows:
  49. .. code-block:: yaml
  50. # app/config/config.yml
  51. fos_user:
  52. #...
  53. from_email:
  54. address: noreply@example.com
  55. sender_name: Demo App
  56. The bundle also provides the flexibility of allowing you to configure the sender
  57. email address for the emails individually.
  58. To configure the sender email address for the user registration confirmation
  59. email update your ``fos_user`` config as follows:
  60. .. code-block:: yaml
  61. # app/config/config.yml
  62. fos_user:
  63. #...
  64. registration:
  65. confirmation:
  66. from_email:
  67. address: registration@example.com
  68. sender_name: Demo Registration
  69. You can similarly update the ``fos_user`` config to change the sender email address for
  70. the password reset request email:
  71. .. code-block:: yaml
  72. # app/config/config.yml
  73. fos_user:
  74. #...
  75. resetting:
  76. email:
  77. from_email:
  78. address: resetting@example.com
  79. sender_name: Demo Resetting
  80. Sending HTML mails
  81. ------------------
  82. The default mailer only supports sending plain text messages. If you want
  83. to send multipart messages, the easiest solution is to use the TwigSwiftMailer
  84. implementation instead. It expects your twig template to define 3 blocks:
  85. - ``subject`` containing the email subject
  86. - ``body_text`` rendering the plain text version of the message
  87. - ``body_html`` rendering the html mail
  88. Here is how you can use it, you can use either of the two methods
  89. of referencing the email template below.
  90. .. code-block:: yaml
  91. # app/config/config.yml
  92. fos_user:
  93. # ...
  94. service:
  95. mailer: fos_user.mailer.twig_swift
  96. resetting:
  97. email:
  98. template: email/password_resetting.email.twig
  99. registration:
  100. confirmation:
  101. template: FOSUserBundle:Registration:email.txt.twig
  102. .. code-block:: html+jinja
  103. {# app/Resources/views/email/password_resetting.email.twig #}
  104. {% block subject %}Resetting your password{% endblock %}
  105. {% block body_text %}
  106. {% autoescape false %}
  107. Hello {{ user.username }} !
  108. You can reset your password by accessing {{ confirmationUrl }}
  109. Greetings,
  110. the App team
  111. {% endautoescape %}
  112. {% endblock %}
  113. {% block body_html %}
  114. {#
  115. You can of course render the html directly here.
  116. Including a template as done here allows keeping things DRY by using
  117. the template inheritance in it
  118. #}
  119. {% include 'email/password_resetting.html.twig' %}
  120. {% endblock %}
  121. .. note::
  122. The HTML part is set in the message only when the ``body_html`` block is
  123. not empty.
  124. You can view the default email templates at
  125. `FOSUserBundle:Registration:email.txt.twig` and
  126. `FOSUserBundle:Resetting:email.txt.twig`
  127. Using A Custom Mailer
  128. ---------------------
  129. The default mailer service used by FOSUserBundle relies on the Swiftmailer
  130. library to send mail. If you would like to use a different library to send
  131. emails, want to send HTML emails or simply change the content of the email you
  132. may do so by defining your own service.
  133. First you must create a new class which implements ``FOS\UserBundle\Mailer\MailerInterface``
  134. which is listed below.
  135. .. code-block:: php
  136. <?php
  137. namespace FOS\UserBundle\Mailer;
  138. use FOS\UserBundle\Model\UserInterface;
  139. /**
  140. * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  141. */
  142. interface MailerInterface
  143. {
  144. /**
  145. * Send an email to a user to confirm the account creation
  146. *
  147. * @param UserInterface $user
  148. */
  149. function sendConfirmationEmailMessage(UserInterface $user);
  150. /**
  151. * Send an email to a user to confirm the password reset
  152. *
  153. * @param UserInterface $user
  154. */
  155. function sendResettingEmailMessage(UserInterface $user);
  156. }
  157. After you have implemented your custom mailer class and defined it as a service,
  158. you must update your bundle configuration so that FOSUserBundle will use it.
  159. Simply set the ``mailer`` configuration parameter under the ``service`` section.
  160. An example is listed below.
  161. .. code-block:: yaml
  162. # app/config/config.yml
  163. fos_user:
  164. # ...
  165. service:
  166. mailer: app.custom_fos_user_mailer
  167. To see an example of a working implementation of the ``MailerInterface``
  168. see the `ZetaMailer`_ class of the `ZetaWebmailBundle`_. This implementation
  169. uses the Zeta Components Mail to send emails instead of Swiftmailer.
  170. .. _ZetaMailer: https://github.com/simplethings/ZetaWebmailBundle/blob/master/UserBundle/ZetaMailer.php
  171. .. _ZetaWebmailBundle: https://github.com/simplethings/ZetaWebmailBundle