FOSUserBundle的邮件服务

原文地址: http://symfony.com/doc/current/bundles/FOSUserBundle/emails.html

FOSUserBundle内置了两种不同的发送邮件的实例.

注册确认

在一个新用户注册完成之前, 如果在FOSUserBundle中配置了必须通过邮件确认的步骤, 系统会发送一封带有链接的邮件到用户邮箱中. 用户访问链接之后会对用户账户进行验证并且激活.

通过邮件验证新用户注册默认是关闭的. 若要启用, 则需修改配置文件:

1
2
3
4
5
6
# app/config/config.yml
fos_user:
# ...
registration:
confirmation:
enabled: true

重置密码

当用户需要重置密码的时候, 通常也是通过发送邮件的方式进行的. FOSUserBundle提供了一个通过两个步骤重置密码的功能. 首先, 用户必须发送一个重置密码的请求. 在发送完请求之后, 会发送一封带有访问链接的邮件. 当访问链接时, 用户会通过被包含在url中的token识别出来. 当用户访问了链接并且token被验证之后, 一个要求输入新密码的表单将呈现在用户面前.

默认的发送邮件实现

FOSUserBundle带有三种邮件实现. 他们的服务id如下:

  • fos_user.mailer.default使用Swiftmailer来发送邮件的默认实现.
  • fos_user.mailer.twig_swift使用Swiftmailer发送邮件并且用twig区块来渲染消息.
  • fos_user.mailer.noop是一个不执行任何操作的邮件实现, 所以他不会发送邮件.

fos_user.mailer.noop邮件服务是在你不需要发送邮件服务, 并且不在应用中安装SwiftmailerBundle的时候使用的. 如果你留下了默认的邮件配置, 并且没有注册SwiftmailerBundle, 你会收到一个依赖缺失的异常.

配置发送邮件的地址

FOSUserBundle默认的邮件服务允许你设置发送邮件的邮件地址. 你可以配置全局的邮件地址, 也可以为每一封邮件配置地址.

若要为所有发送出去的邮件设置地址, 仅需像下面这样更新你的fos_user配置:

1
2
3
4
5
6
# app/config/config.yml
fos_user:
#...
from_email:
address: noreply@example.com
sender_name: Demo App

FOSUserBundle也提供了为每一封邮件设置邮件地址的灵活性.

若要为注册确认邮件设置地址, 仅需像下面这样更新你的fos_user配置:

1
2
3
4
5
6
7
8
# app/config/config.yml
fos_user:
#...
registration:
confirmation:
from_email:
address: registration@example.com
sender_name: Demo Registration

你也可以通过配置fos_user来设置重置密码请求发送出去的邮件地址:

1
2
3
4
5
6
7
8
# app/config/config.yml
fos_user:
#...
resetting:
email:
from_email:
address: resetting@example.com
sender_name: Demo Resetting

发送HTML邮件

默认的邮件服务只是单纯的发送文本信息. 如果你想发送复合信息, 最简单的解决方案就是使用TwigSwiftMailer实现. 他为你的twig模板定义了三个区块:

  • subject 包含了邮件的主题
  • body_text 渲染纯文本版本的消息
  • body_html渲染html邮件

下面是如何使用的方法, 你可以参考下面两种引用邮件模板文件的方法.

1
2
3
4
5
6
7
8
9
10
11
# 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{# 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 %}

HTML部分仅在body_html区块不为空的情况下才会显示在信息中.

你可以在FOSUserBundle:Registration:email.txt.twig和FOSUserBundle:Resetting:email.txt.twig中查看默认的邮件模板.

使用自定义邮件服务

FOSUserBundle使用的默认的邮件服务依赖于Swiftmailer类库. 如果你想使用其他的类库来发送邮件, 发送HTML邮件或者简单的修改邮件内容, 你可以向下面这样来定义你自己的邮件服务.

首先你得创建一个实现了FOS\UserBundle\Mailer\MailerInterface接口的类. 接口如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php

namespace FOS\UserBundle\Mailer;

use FOS\UserBundle\Model\UserInterface;

/**
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
*/
interface MailerInterface
{
/**
* Send an email to a user to confirm the account creation
*
* @param UserInterface $user
*/
function sendConfirmationEmailMessage(UserInterface $user);

/**
* Send an email to a user to confirm the password reset
*
* @param UserInterface $user
*/
function sendResettingEmailMessage(UserInterface $user);
}

在你实现了你的自定义邮件类并且将它定义成了服务之后, 你需要更新你的配置文件, 以便FOSUserBundle能够正确的使用. 只要设置在service部分的mailer参数即可. 就像下面这样:

1
2
3
4
5
# app/config/config.yml
fos_user:
# ...
service:
mailer: app.custom_fos_user_mailer

你可以查看ZetaWebmailBundle中的ZetaMailer类是如何实现MailerInterface的. 这个实现使用了 Zeta Components Mail 来发送邮件, 而不是Swiftmailer.