Returning an error that no mail was sent in Yii 2 mailbox class

I am using Yii 2

and using the mailer from the mailer base class and works fine for the most part, but sometimes it fails to send ..... so my code is like:

// Let start composing the message
$mail = Yii::$app->mailer->compose($view_data, $view_params);

//.........

// Send the message
$send = $mail->send();      

      

Sometimes $send

there is false

, but I'm not sure how you know WHY it is false? Is there a way to get the error by making it false?

+3


source to share


2 answers


If you are sending multiple recipient messages, then you should check for failures: http://swiftmailer.org/docs/sending.html#getting-failures-by-reference for example.

if (Yii::$app->mailer->getSwiftMailer()->send($message->getSwiftMessage(), $failures))    
{
    // do what you want with $failures var
}

      



And if you really want to handle smtp errors in your yii application you should use the swiftmailer logger plugin: http://swiftmailer.org/docs/plugins.html#logger-plugin

PS: first I would check the yii and smtp logs to see what errors are being thrown ...

+3


source


Can't comment, but you need more information about your Yii setup.

Have a look at the code base for a basic mailer here .... https://github.com/yiisoft/yii2/blob/master/framework/mail/BaseMailer.php



You can see that if useFileTransport = true, it tries to save the message to the file system. otherwise, it calls the abstract method.

This means we need to know which 3rd Party Mailer you are using.

-1


source







All Articles