Cron Job一次只发送一封电子邮件

时间:2014-05-28 08:27:20

标签: php cron

我为cron作业创建了一个php文件。我想通过此cron作业向用户发送讲座详细信息。代码如下:

 <?
 $login=mysql_query("select * from table1 WHERE member_status = '1'  AND email_lecture_sent ='0' "); 
 $datelogin=mysql_fetch_array($login);

 $current_date=date("Y-m-d H:i:s a");   
 $objTotalRS=mysql_query("select * from  table2 where display_time >'".$current_date."' order by lecture_start asc limit 0,1");


  while ($data=mysql_fetch_array($objTotalRS))  {

        $date1= date("Y-m-d");
        $date2=date("Y-m-d",strtotime($data['lecture_start']));

        $dateDiff = strtotime($date2) - strtotime($date1);
        $fullDays = floor($dateDiff/(60*60*24));

          if($datelogin['email_lecture_sent'] = '0'){
    mysql_query(" UPDATE table1 SET email_lecture_sent = '1' WHERE reg_id =".$datelogin['reg_id']);

    }

    if($fullDays=="1" || $fullDays=="2"){
    ob_start(); 
       ?>

         email content containing lecture details along with user's data like username, password etc.. .


 <?
    $body=ob_get_contents();
    ob_end_clean ();

    $to = $datelogin["email"];
    $sub = "Our Next Live Session";



    $mail->IsHTML(true); 
    $mail->Host = "mail.abc.com";
    $mail->From = "info@abc.com"; 
    $mail->FromName = "ABC"; 

    $mail->AddAddress($to);
    $mail->Subject = $sub;
    $mail->Body = $body;
    $mail->WordWrap = 50;
    if($mail->Send()){
    echo"Success...";
    }else{
        echo"oops";
    }


    $mail->ClearAddresses();
    $mail->ClearCCs();
    $mail->ClearBCCs();

        }

     }
         ?>

但问题是当我运行这个文件时,它只在一个cron作业中发送一封电子邮件,而不是发送给member_status =&#39; 1&#39;和email_lecture_sent =&#39; 0&#39;

2 个答案:

答案 0 :(得分:0)

请删除代码中的ob_start();功能。 Bcz,如果你使用ob_start()函数,这意味着你再次启动所有对象。

if($fullDays=="1" || $fullDays=="2"){
ob_start(); ///// Remove this line.
   ?>
     email contents...
   <?
   rest email sending php code....
   }
}

感谢。

答案 1 :(得分:0)

我按照以下代码和重新设计的代码工作,这对我来说非常有用..

更新的代码是 -

<?php
$log= "select * from table1 WHERE member_status = '1' AND email_lecture_sent='0'";
$login=mysql_query($log);

while ($datelogin=mysql_fetch_array($login))  {

    mysql_query(" UPDATE table1 SET email_lecture_sent = '1' WHERE reg_id =".$datelogin['reg_id']);

    $current_date=date("Y-m-d H:i:s a");
    $objTotalRS=mysql_query("select * from  table2 where display_time >'".$current_date."' order by lecture_start asc limit 0,1");

    $data=mysql_fetch_assoc($objTotalRS);

        $date1= date("Y-m-d");
        $date2=date("Y-m-d",strtotime($data['lecture_start']));
        $dateDiff = strtotime($date2) - strtotime($date1);
        $fullDays = floor($dateDiff/(60*60*24));


         if ($fullDays=="2" || $fullDays=="1") {
          ob_start();
?>  

上面的代码

感谢您的支持