问题
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