Yii2 How to handle Swift_TransportException?

When I try to send mail using swiftmailer, I get the following error:

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 put swiftmailer in a try catch block like this:

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 no exception was thrown.

How can I handle this exception?

+3


source to share


1 answer


Good. I needed to use \Swift_TransportException

in catch.



+3


source







All Articles