本文介绍了如何在 Silex 中处理 Swift_TransportException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Silex 中捕获 SwiftMailer 的异常时遇到了一个奇怪的问题.我想发送这样的电子邮件:

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');
}

我知道它不会在本地主机上发送任何电子邮件,我预计它会失败,但为什么我无法在 try - catch 中捕获 Swift_TransportException 异常阻止?

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?

它只是打印:

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

推荐答案

我也无法在本地主机上发送邮件 -我用这段代码摆脱了异常:

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']
));

这篇关于如何在 Silex 中处理 Swift_TransportException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 18:14