I have a code like below in yii.
<?php
class MediaController extends Controller {
public $mail_try = 1;
public function actionUpdate()
{
// ........... Other Code
$return_err = array();
/* Now Send Email */
if(sizeof($mail_queue)>0)
{
foreach($mail_queue as $resmail)
{
$this->mail_try = 1;
$to_arr = $resmail['to_arr'];
$cc_arr = $resmail['cc_arr'];
$from = $resmail['from'];
$subject = $resmail['subject'];
$message = $resmail['message'];
$log_msg = $resmail['log_msg'];
$attachment = $resmail['attachment'];
$log = $resmail['log'];
$output = $this->mailsend($to_arr, $cc_arr, $from, $subject, $message, 'Image/Media update (action : insert) ', $attachment,$log);
if($output==0)
{
$return_err[] = $resmail['vendor_name'];
}
}
}
}
public function mailsend($to, $cc, $from, $subject, $message, $crontask, $attachment = array(),$log='') {
//....... Other Code
//....... Other Code
try{
if (!$mail->Send()) {
if($this->mail_try < 3)
{
$this->mail_try++;
$this->mailsend($to, $cc, $from, $subject, $message, $crontask,$attachment,$log);
return 0;
}
} else {
return 1;
}
} catch (Exception $ex) {
return 0;
}
}
}
?>
What i am trying to do is if mail sending is failed, then call the same function to retry to send the email. If email sending is still failed then return 0 else 1. Then using this return value i am trying to notify the user about mail sending error.
Earlier, i thought that code below the return value will not execute. But i am wrong. In above case it execute after the return value, because it is in recursive function.
So, How to solve this problem?