在开发模式下在 Symfony 3 中发送电子邮件

Sending Email in Symfony 3 in dev mode

需要帮助学习如何使用带 Swift_mailer 库的 Symfony 框架在开发模式下发送电子邮件。

这是我的 config_dev.yml 文件:

swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }

还有我的 config.yml 文件:

swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }

还有我的 parameters.yml 文件:

....
mailer_transport: gmail
mailer_host: 127.0.0.1
mailer_user: 'my_gamil_here'
mailer_password: 'my_password_here'

然后在我的控制器中我有以下内容:

public function indexAction(\Swift_Mailer $mailer)
{
    $message = (new \Swift_Message('Hello Email'))
        ->setFrom('westtexascentral555@gmail.com')
        ->setTo('kaleyaw@yahoo.com')
        ->setBody(
            $this->renderView(
                // app/Resources/views/Emails/registration.html.twig
                'Emails/registration.html.twig',
                array('name' => 'James')
            ),
            'text/html'
        );

    $mailer->send($message);

    return new Response('Email sent');
}

有人能告诉我哪里出了问题吗?没有任何内容发送到电子邮件帐户。

'mailer_transport' 是发送邮件的协议。
尝试使用 'smtp' 而不是 'gmail' :)

可能是因为您将邮件主机指定为本地主机。检查这个 symfony 文档 http://symfony.com/doc/current/email/gmail.html。并注意这部分:

The gmail transport is simply a shortcut that uses the smtp transport and sets these options:

encryption  ssl

auth_mode   login

host    smtp.gmail.com
  1. 确保 mailer_user 仅设置为您的 gmail 用户名(没有主机部分)。假设我有电子邮件 myemail@gmail.com,我的用户名将是 myemail

  2. 您是否对您的 gmail 帐户使用两步验证?如果是这样,您还需要为您的帐户生成应用密码,如 docs:

  3. 中指定

If your Gmail account uses 2-Step-Verification, you must generate an App password and use it as the value of the mailer_password parameter. You must also ensure that you allow less secure apps to access your Gmail account.

稍后,您可以关注 here、登录、select 应用程序(在本例中为 Mail)和您要使用邮件程序的设备,然后按 Generate你会看到带有你的应用程序密码的模态,类似于 snub udpz xcvt jrdp。复制此密码,并将其用作您的 gmail 密码(不带空格)。

如果这没有帮助,还要检查日志,通常如果 smtp 服务器有问题,swiftmailer 会将错误打印到日志中。