> Symfony中文手册 > 如何发送邮件

如何发送邮件

在任何Web程序开发中,发送邮件都是个经典任务,而且格外复杂、充满陷阱。不去发明轮子的一个邮件发送解决方案是,使用swiftmailerBundle,它发挥了Swift Mailer类库的威力。这个bundle已被内置于Symfony标准版框架之中。

配置 ¶

要使用Swift Mailer,你需要配置它以便在邮件服务器上能够使用。

有别于设置/使用你自己的邮件服务器,你可能希望选择一个邮件托管服务商比如Mandrill, SendGrid, Amazon SES或其他品牌。它们会给你提供一个SMTP服务器,用户名以及密码(一般被称为keys),这些都是要用到Swift Mailer配置中的。

在标准版Symfony的安装中,一些swiftmailer配置信息已经被包括进来:

1
2
3
4
5
6
# app/config/config.yml
swiftmailer:
    transport: '%mailer_transport%'
    host:      '%mailer_host%'
    username:  '%mailer_user%'
    password:  '%mailer_password%'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="Http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/swiftmailer http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd">
 
    <swiftmailer:config
        transport="%mailer_transport%"
        host="%mailer_host%"
        username="%mailer_user%"
        password="%mailer_password%"
    />
</container>
1
2
3
4
5
6
7
// app/config/config.php
$container->loadFromExtension('swiftmailer', array(
    'transport'  => "%mailer_transport%",
    'host'       => "%mailer_host%",
    'username'   => "%mailer_user%",
    'password'   => "%mailer_password%",
));

这些值(例如%mailer_transport%),是从parameters.yml文件中所设置的参数中读出来的。你可以在那个文件中修改这些值,或是直接在这里进行设置。

下列配置属性都是可用的:

  • transport (smtp, mail, sendmail, or gmail)
  • username
  • password
  • host
  • port
  • encryption (tls, or ssl)
  • auth_mode (plain, login, or cram-md5)
  • spool
    • type (如何对信息(messages)进行排序,filememory 都是被支持的,参考 如何滚动发送邮件)
    • path (存储信息的路径)
  • delivery_address (一个邮件地址,用于发送所有邮件)
  • disable_delivery (设为true即可彻底关闭邮件发送)

发送邮件 ¶

Swift Mailer类库在工作时,依照创建、配置、然后发送Swift_Message对象来进行。“mailer”负责的是信息的真实发送,它可以通过mailer服务来得到。总地说,发送邮件相当直接了当:

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
26
27
28
29
30
public function indexAction($name)
{
    $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('send@example.com')
        ->setTo('recipient@example.com')
        ->setBody(
            $this->renderView(
                // app/Resources/views/Emails/registration.HTML.twig
                'Emails/registration.html.twig',
                array('name' => $name)
            ),
            'text/html'
        )
        /*
         * If you also wAnt to include a plaintext version of the message
         * 如果你同时希望包容一个“纯文本”版本的信息
        ->addPart(
            $this->renderView(
                'Emails/registration.txt.twig',
                array('name' => $name)
            ),
            'text/plain'
        )
        */
    ;
    $this->get('mailer')->send($message);
 
    return $this->render(...);
}

为了实现松耦合,邮件本体(body)被存储到一个模板中,并使用renderView()方法进行渲染。registration.html.twig模板可能像下面这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{# app/Resources/views/Emails/registration.html.twig #}
<h3>You did it! You registered!</h3>
 
Hi {{ name }}! You're successfully registered.
 
{# example, assuming you have a route named "login" #}
{# 示例,假设你有一个名为"login"的路由 #}
To login, go to: <a href="{{ url('login') }}">...</a>.
 
Thanks!
 
{# Makes an absolute URL to the /images/logo.png file #}
{# 确保“/images/logo.png file”是一个绝对路径 #}
<img src="{{ absolute_url(asset('images/logo.png')) }}">

$message对象支持许多选项,例如包容附件,添加HTML内容,等等。幸运的是,Swift Mailer在它自己的文档中涵盖了Creating Messages(创建信息)的相关内容。