Yii2如何处理Swift_TransportException异常?

时间:2015-05-07 06:15:42

标签: exception-handling yii2 swiftmailer yii2-advanced-app

尝试使用swiftmailer发送邮件时出现以下错误:

Expected response code 250 but got code "550", with message "550 User test@test.com has exceeded its 24-hour sending limit. Messages to 250 recipients out of 250 allowed have been sent. Relay quota will reset in 1.47 hours.

我已将swiftmailer放在try catch块中,如下所示:

public function sendMail($emailTemplateName, $subject, $body, $fromEmail, $toEmail)
{
        try
        {
            return Yii::$app->mailer->compose(['html' => $emailTemplateName], ['emailBody' => $body])
                ->setFrom([ $fromEmail => Yii::$app->params['siteTitle'] ])
                ->setTo($toEmail)
                ->setSubject($subject)
                ->send();
        }
        catch(Swift_SwiftException $exception)
        {
            return 'Can sent mail due to the following exception'.print_r($exception);
        }
}

我也尝试过使用Swift_SwiftExceptionException,但是没有被捕获。

如何处理此异常?

1 个答案:

答案 0 :(得分:3)

好的,明白了。我需要在catch中使用\Swift_TransportException