gpt4 book ai didi

php - Symfony2 Controller 中未捕获 Swiftmailer 异常

转载 作者:可可西里 更新时间:2023-11-01 12:37:37 25 4
gpt4 key购买 nike

我正在开发一个简单的邮件应用程序,使用 Gmail 帐户作为主机。它工作起来很有魅力,但是当 send() 函数抛出异常时问题就出现了。我看到 try catch 语句无法处理异常。即使我使用全局异常类,它也不起作用。这个问题也在某处讨论过。

例如:

Catch swiftmailer exception in Symfony2 dev env controller

https://groups.google.com/forum/#!topic/symfony2/SlEzg_PKwJw

但他们没有找到有效的答案。

我的 Controller 功能代码是:

    public function ajaxRegisterPublisherAction()
{

//some irrelevant logic

$returns= json_encode(array("username"=>$username,"responseCode"=>$responseCode));


try{
$message = \Swift_Message::newInstance()
->setSubject('hello world')
->setFrom('jafarzadeh91@gmail.com')
->setTo('jafarzadeh991@yahoo.com')
->setBody(
$this->renderView('AcmeSinamelkBundle:Default:email.txt.twig',array('name'=>$username,"password"=>$password))
);
$this->container->get("mailer")->send($message);

}
catch (Exception $e)
{

}



return new \Symfony\Component\HttpFoundation\Response($returns,200,array('Content-Type'=>'application/json'));


}

我在 Firebug 控制台收到的从上面的代码发送的响应是:

{"username":"xzdvxvvcvxcv","responseCode":200}<!DOCTYPE html>
<html>
.
.
Swift_TransportException: Connection could not be established with host smtp.gmail.com [Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?]
.
.
</html>

我抓狂了,因为我不知道为什么内核会在我的 json 对象的 continue 中处理异常?

当我评论这一行时:

 $this->container->get("mailer")->send($message);

异常没有发生,我在客户端有一个有效的 json。(这是理所当然的)我将 Exception 更改为 \Exception\Swift_TransportException 甚至 Swift_TransportException !但没有好的结果。

最佳答案

当您执行 $this->container->get("mailer")->send($message); 时,如果您启用了假脱机功能,则此时不会发送电子邮件.参见 http://symfony.com/doc/current/cookbook/email/spool.html

如果您有 spool: { type: memory } 的默认设置,则 \Swift_TransportException 将在您的 Controller 退出后的内核终止阶段抛出。解决此问题的一种方法是关闭假脱机(但是您的用户可能必须在发送电子邮件时等待),或者您可以创建自己的事件监听器来处理异常。 http://symfony.com/doc/current/cookbook/service_container/event_listener.html

关于php - Symfony2 Controller 中未捕获 Swiftmailer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18215668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com