dongwu3747 2014-05-28 08:27
浏览 59
已采纳

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

I have created a php file for cron job. I want To send Lecture details To Users Through this cron job. The code is as follows :

 <?
 $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();

        }

     }
         ?>

BUT problem is when I run this file, it sends only one email in one cron job and not to all with member_status='1' and email_lecture_sent='0'

  • 写回答

2条回答 默认 最新

  • dongrouli2667 2014-05-28 16:01
    关注

    I worked on above code and redesigned code as follows which worked for me perfectly..

    updated code is -

    <?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();
    ?>  
    

    Rest code as above

    Thank you for support

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!