如何在Swiftmailer中捕获由无效凭证或smtp超时产生的异常?

时间:2019-01-13 21:53:27

标签: php symfony swiftmailer

请告知如何捕获以下SwiftMailer异常:

[2019-01-13 15:14:45] app.ERROR: Exception occurred while flushing email queue: Connection could not be established with host smtp.gmail.com [php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution #0] 

由于凭据无效而重现此异常,但是我没有运气将其捕获到try-catch块中。

 try {
    $this->logger->info('Sending email...');
    $this->mailer->send($swiftMessage);
 } catch (Exception $e) {
    $this->logger->error('Email sending fails with Error: '.$e->getMessage());
    throw $e;
 }
 $this->logger->info(__CLASS__.': Email successfully sent');

因此凭据是否有效并不重要-它始终记录成功发送电子邮件的情况。

1 个答案:

答案 0 :(得分:0)

捕获\ Error而不是\ Exception。