0

we are using phpmailer function for sending mail inside of while loop. We have created a function for sending mail and we are using that function inside of while loop but while loop is not executing till the condition becomes false, but whenever we are not using mail function while loop is executed properly. Can anyone please help me. Thanks in advance

 $sql_get_email_type = "SELECT * FROM tb_add_email_type";
    $result_get_email_type = sqlsrv_query($con, $sql_get_email_type);
    while($rs_get_email_type = sqlsrv_fetch_array($result_get_email_type, SQLSRV_FETCH_ASSOC))
    {
        $added_email_type=$rs_get_email_type['email_type'];
        $added_table_name=$rs_get_email_type['table_name'];
        $added_column_name=$rs_get_email_type['column_name'];
        $added_column_title=$rs_get_email_type['column_title'];
        echo $added_email_type;

        $sql_dispatch = "SELECT * FROM tb_date_range WHERE email_type='$added_email_type'";
        $result_dispatch = sqlsrv_query($con, $sql_dispatch) or die(sqlsrv_errors($con));
        $rs_dispatch = sqlsrv_fetch_array($result_dispatch);
        $schedule_type=$rs_dispatch['schedule_type'];
        $scheduled_date=$rs_dispatch['scheduled_date'];
        $provided_days_count=$rs_dispatch['provided_days_count'];

        $sql1 = "SELECT DISTINCT(custname),to_parent FROM $added_table_name";
        $result1 = sqlsrv_query($con, $sql1) or die(sqlsrv_errors($con));
        while($rs = sqlsrv_fetch_array($result1))
        {
            $customer_name=$rs['custname'];
            $email=$rs['to_parent'];
            $message ='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01  
            Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
            <html lang="en"> 
            <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
            <title>Send Mail</title>
            <link rel="shortcut icon" type="image/x-icon" href="../assets/images/c_icon.png" />
            <link rel="stylesheet" href="../assets/vendor/bootstrap/css/bootstrap.min.css">
            <link href="../assets/vendor/fonts/circular-std/style.css" rel="stylesheet">
            <link rel="stylesheet" href="../assets/libs/css/style.css">
            <link rel="stylesheet" href="../assets/vendor/fonts/fontawesome/css/fontawesome-all.css">
            <link rel="stylesheet" type="text/css" href="../assets/vendor/datatables/css/dataTables.bootstrap4.css">
            <link rel="stylesheet" type="text/css" href="../assets/vendor/datatables/css/buttons.bootstrap4.css">
            <link rel="stylesheet" type="text/css" href="../assets/vendor/datatables/css/select.bootstrap4.css">
            <link rel="stylesheet" type="text/css" href="../assets/vendor/datatables/css/fixedHeader.bootstrap4.css">
            </head>
            <body> 
            <div class="card" style="padding:20px;margin:20px;border-radius:10px;" id="custdata_updtdiv">
            <span>';
            $query_header = "SELECT * FROM tb_date_range where email_type='$added_email_type'";
            $search_result_header = sqlsrv_query($con, $query_header) or die(sqlsrv_errors($con));
            while($row_header = sqlsrv_fetch_array($search_result_header))
            {
                $header = $row_header['mail_header'];
                $header1 = explode('|', $header);
                $headernew = "";
                foreach($header1 as $str)
                {
                    $headernew = $headernew. "<br/>" . $str;
                }
                $footer = $row_header['mail_footer'];
                $footer1 = explode('|', $footer);
                $footernew = "";
                foreach($footer1 as $str)
                {
                    $footernew = $footernew. "<br/>" . $str;
                }
                $subject = $row_header['mail_subject'];
            }
            $message .= '
            '.$headernew.'
            </span><br/><br/>
            <table align="center" cellpadding="0" cellspacing="0" border="1"> 
            <tr style="color:#000000;">';
            $array = explode(',', $added_column_title); //split string into array seperated by ', '
            foreach($array as $value) //loop over values
            {
                $message .= ' <th style="text-align:center; background:#999999;padding:5px;">'.$value.'</th> ';
            }
            $message .='
            </tr> '; // END $message for a second while we query 
            $sql = "SELECT * FROM {$added_table_name} where custname='$customer_name'";
            $result = sqlsrv_query($con, $sql) or die(sqlsrv_errors($con));
            while($rs=sqlsrv_fetch_array($result))
            {
                $message .= ' 
                <tr style="color:#000000;"> ';
                $array = explode(',', $added_column_name); //split string into array seperated by ', '
                foreach($array as $value_td) //loop over values
                {                                                       
                    $message .= '
                    <td  style="text-align:center;padding:5px;">'.$rs[$value_td].'</td> ';
                }
                $message .= '
                </tr> '; 
            }  
            $message .= ' 
            </table> <br/>
            <p>
            '.$footernew.'
            </p> 
            </div>
            </body> 
            </html> ';

            if($schedule_type=="Daily")
            {
                sendmail($message,$subject,$email);
                date_default_timezone_set('Asia/Calcutta');
                $current_date=date("Y-m-d");
                $scheduled_date=date('Y-m-d', strtotime(date("Y-m-d", strtotime($current_date)). ' + 1 days'));
                sqlsrv_query($con, "update tb_date_range set scheduled_date='$scheduled_date' where email_type='$added_email_type' and schedule_type='Daily'") or die(sqlsrv_errors()); 
            }
        }
    }

    function sendmail($message,$subject,$email)
    {
        require_once("class.phpmailer.php");
        require_once("class.smtp.php");
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->Host = "Hostname";
        $mail->SMTPAuth = true;
        $mail->Port = 25;
        $mail->Username = "Username";
        $mail->Password = "Password";
        $mail->From = "From";
        $mail->FromName = "Fromname";
        $addresses = explode(',', $email);
        foreach ($addresses as $address) {
            $mail->AddAddress($address);
        }  
        $mail->IsHTML(true);
        $mail->Subject = $subject;
        $mail->Body = $message;

        if(!$mail->Send())
        {
            echo 'Mail Not Send';
            //echo 'Mailer Error: ' . $mail->ErrorInfo;
        }
        else
        {
            echo 'Mail Send';
        }   
    }
shiba
  • 1
  • 2
  • just add echo before sendmail to output all params you sending to function. Probably email is null or smth – Augustas Sep 17 '19 at 06:48
  • Did you follow these ? https://stackoverflow.com/questions/2639474/sending-200-emails-using-php-mail-function-in-a-loop https://stackoverflow.com/questions/36466830/sendgrid-php-loop-not-working-only-sends-one-email https://stackoverflow.com/questions/25870594/php-mail-function-waits-and-not-working-with-foreach-loop?noredirect=1&lq=1 – Paul P Jose Sep 17 '19 at 06:52
  • This is a very inefficient way of sending. I'd recommend you follow [the mailing list example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps). You're also using an old version of PHPMailer, so [update](https://github.com/PHPMailer/PHPMailer/). – Synchro Sep 17 '19 at 07:38
  • Thank u all for replies. I am trying one by one solutions you provided. Will let u know if this solves my issue – shiba Sep 17 '19 at 08:13

0 Answers0