Yii2如何处理Swift_TransportException异常?

时间:2022-10-05 09:39:37

When trying to send mail using swiftmailer I am getting the following error:

当尝试使用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.

I have put the swiftmailer in the try catch block as follows:

我已将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);
        }
}

I also tried with Swift_SwiftException and Exception but exeption doesn't get caught.

我也试过Swift_SwiftException和Exception但是exeption没有被捕获。

How can I handle this exception?

我该如何处理这个异常?

1 个解决方案

#1


Ok got it. I needed to use \Swift_TransportException in catch.

好的,我知道了。我需要在catch中使用\ Swift_TransportException。

#1


Ok got it. I needed to use \Swift_TransportException in catch.

好的,我知道了。我需要在catch中使用\ Swift_TransportException。