How to handle Swift_TransportException in Silex

删除回忆录丶 提交于 2019-12-21 06:06:49

问题


I'm having quiet weird problem with catching SwiftMailer's exceptions in Silex. I want to send an email like this:

try {
    $message = \Swift_Message::newInstance()
        ->setSubject('subject')
        ->setFrom(array('form'))
        ->setTo(array('to'))
        ->setBody('body');
    $app['mailer']->send($message);
} catch (\Swift_TransportException $e) {
    $app['logger']->addError('Unable to send welcome email');
}

I know it's not going to send any email on localhost and I'm expecting it to fail but why I can't catch the Swift_TransportException exception in try - catch block?

It just prints:

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host localhost [Connection refused #61]'

回答1:


Emails are not sent immediately when you call send(), instead they get into a memory spool that is flushed during the application shutdown, when the response has already been sent. This improves the user experience, as the response can be sent much faster.




回答2:


i had trouble sending mails on my localhost too - i got rid of the exception with this code:

/// config
$app['swiftmailer.options'] = array(
    'host' => 'smtp.1und1.de',
    'port' => '465',
    'username' => 'xxx',
    'password' => 'yyy',
    'encryption' => 'ssl',
    'auth' => 'login',
);

// bootstrap
$app->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
    'swiftmailer.options' => $app['swiftmailer.options']
));


来源:https://stackoverflow.com/questions/17793096/how-to-handle-swift-transportexception-in-silex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!