如何调试Symfony2应用程序中的电子邮件发送


How can I debug email sending within my Symfony2 application?

我正在Symfony2应用程序中使用FOSUser捆绑包。我正在尝试使电子邮件功能正常工作,但无法成功发送任何电子邮件。

我的开发环境是一个Ubuntu虚拟机。我尝试使用我的Gmail帐户详细信息,如这里所述。我也检查了错误日志,但没有任何关于电子邮件发送的记录。

我尝试使用测试控制器发送测试电子邮件;

use Symfony'Bundle'FrameworkBundle'Controller'Controller;
class TestEmailController extends Controller
{
    public function sendEmailAction()
    {
        $message = 'Swift_Message::newInstance()
            ->setSubject('Hello Email')
            ->setFrom('send@example.com')
            ->setTo('me@myemailaddress.com') // use a valid email in actual code...
            ->setBody('yo, wassup!');
        $this->get('mailer')->send($message);
        return $this->render('SysDevPunctualityBundle:TestEmail:sendEmail.html.twig', array(
                // ...
        ));    
    }
}

然而,这会触发Symfony的web探查器工具栏出现错误,我收到以下消息;在数据库中找不到令牌"3c494e"。

经过一番挖掘,我发现了这个问题,我遵循了删除spool选项的建议,还添加了"from_email"选项。这产生了不同,我现在得到了一个超时错误:

Connection could not be established with host smtp.gmail.com [Connection timed out #110] 

我的猜测是防火墙正在阻止请求,我不知道如何确定。

如果有人能为我指明正确的方向,我将不胜感激。

我发现问题的根源是我的工作防火墙阻止了对Gmail SMTP服务器的访问。我切换到另一个SMTP服务器(由我们的IT部门设置),电子邮件发送现在可以正常工作。

查看食谱中的这两个链接,了解如何在开发时处理电子邮件:http://symfony.com/doc/current/cookbook/email/dev_environment.html&http://symfony.com/doc/current/cookbook/email/spool.html