Let us assume that our email sending logic resides in SiteController.php.
Then we can add a line for error logging in the following way.
if(!$mail->Send())
{
//echo "Mailer Error: " . $mail->ErrorInfo;
Yii::log("Mailer Error: " . $mail->ErrorInfo,'error','application.controllers.SiteController');
//Second parameter is the level of message('error','info',...);
//Third parameter is category which is actually meaning place of logging.
}
else
{
echo "Message sent!";
}
In main configuration file we have to add a new route in log component for logging this errors.
Main.php
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
//The following router application component is added.
array(
'class'=>'CFileLogRoute',
'levels'=>'error',
'categories'=>array('application.controllers.SiteController'),
'logFile'=>'mailError.log',
),
// uncomment the following to show log messages on web pages
/*
array(
'class'=>'CWebLogRoute',
),
*/
),
),
Now any errors is saved in the protected/runtime/mailError.log which is created by YII itself.