How to get mail status messsage in yii2

In Yii2, you can get the mail status message using the following methods:

  1. Using Yii::$app->mailer->getMailStatus():

You can use the getMailStatus() method of the Mailer component to get the mail status message. This method returns an array containing the mail status message and other information.

Example:

$mailStatus = Yii::$app->mailer->getMailStatus();
echo $mailStatus['message']; // prints the mail status message
  1. Using Yii::$app->mailer->getLastError():

You can use the getLastError() method of the Mailer component to get the last error message, which includes the mail status message.

Example:

$errorMessage = Yii::$app->mailer->getLastError();
echo $errorMessage; // prints the mail status message
  1. Using Yii::$app->mailer->getMailLog():

You can use the getMailLog() method of the Mailer component to get the mail log, which includes the mail status message.

Example:

$mailLog = Yii::$app->mailer->getMailLog();
foreach ($mailLog as $log) {
    echo $log['message']; // prints the mail status message
}
  1. Using a custom mail handler:

You can also use a custom mail handler to get the mail status message. For example, you can create a custom mail handler that extends the Mailer component and overrides the send() method to capture the mail status message.

Example:

class CustomMailer extends \yii\mail\Mailer {
    public function send($view, $params = []) {
        $result = parent::send($view, $params);
        $this->mailStatusMessage = $result['message'];
        return $result;
    }
}

Then, in your controller, you can use the custom mail handler and access the mail status message:

$mailer = new CustomMailer();
$mailer->send('your-view', ['params' => 'values']);
echo $mailer->mailStatusMessage; // prints the mail status message

Note that the mail status message is only available after the mail has been sent, so you need to call the send() method and then access the mail status message.