Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 How to handle Swift_TransportException exception?

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

Expected response code 250 but got code "550", with message "550 User [email protected] 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:

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.

How can I handle this exception?

like image 260
Chinmay Waghmare Avatar asked May 07 '15 06:05

Chinmay Waghmare


1 Answers

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

like image 197
Chinmay Waghmare Avatar answered Nov 20 '22 16:11

Chinmay Waghmare