WEB开发网
开发学院WEB开发PHP PHP后台隔5分钟发送email邮件 阅读

PHP后台隔5分钟发送email邮件

 2014-01-08 09:48:22 来源:本站整理   
核心提示: 5分钟发送email,并且邮件内容为由html模板生成的tablewhile(1){//ten minutevar_dump("check task, please don't close");//send emailprocessDBData($db);//process invento

 5分钟发送email,并且邮件内容为由html模板生成的table
while(1)
 {
  //ten minute
  var_dump("check task, please don't close");
  
  //send email
  processDBData($db);
  
  //process inventory state
  processInventoryState($db,$tasks);
  
  sleep(5*60);
 }

//read table data from database
 function processDBData($db)
 {
  $testplan_id = '';
  $temp = '';
  $TotalCaseNum = 0;
  $projectName = '';
   
  //check finished testplan table->tasks
  $sql="SELECT TK.id,TK.send_email,TK.user_id,TK.build_id,TK.case_ids,TK.testplan_id,TK.flag,TK.total
  from tasks AS TK WHERE current=total and flag='2' and ISNULL(send_email)";
  $controlInfor = $db->get_recordset($sql);
  
  if(null== $controlInfor)
  {
   return;
  }

  
  foreach($controlInfor as $key =>$item)
  { 
   //open model html
   $fModel = fopen("report.model","r");
   if(null == $fModel)
   {
    var_dump("no model");
   }
   
   $Modelhandle = fread($fModel,filesize("report.model"));
  
   $test_cases = $item['case_ids'];
   
   //get total Num
   $TotalCaseNum = (int)$item['total'];

   //testplan table->testplan
   //$sql = "SELECT notes,testproject_id FROM testplans WHERE id='{$item['testplan_id']}'";
   $sql = "SELECT name from nodes_hierarchy WHERE id='{$item['testplan_id']}'";
   $result = $db->get_recordset($sql);
   $temp = str_replace("MODEL_TESLPLANE",$result[0]['name'],$Modelhandle);
   $Modelhandle = $temp;
   
   //product table->testprojects
   $sql = "SELECT notes FROM testprojects WHERE id='{$result[0]['testproject_id']}'";
   var_dump($sql);
   $result = $db->get_recordset($sql);
   
   $temp = str_replace("MODEL_PRODUCTNAME",$result[0]['notes'],$Modelhandle);
   $Modelhandle = $temp;
   
   //build table->builds
   $sql = "SELECT DB.name FROM builds AS DB WHERE testplan_id='{$item['testplan_id']}' and id='{$item['build_id']}'";
   $result = $db->get_recordset($sql);
   var_dump($sql);
   $temp = str_replace("MODEL_BUILD",$result[0]['name'],$Modelhandle);
   $projectName = $result[0]['name'];
   $Modelhandle = $temp;
   
   //test result table->executions
   $cases = explode(',',$test_cases);
   $case_infor = get_ts_name_details($db,$cases);
   
   //var_dump(sizeof($case_infor));
   
   //process html testcase content <!--3.testcase content--> nl2br()
   //after the content added(table,body,html end flag)
   var_dump($case_infor);
   
   $BeforSuiteNam = "";
   $successNum = 0;
   $TESTNUM = 0;
   
   //var_dump($case_infor);
   //var_dump($case_infor);
   foreach($case_infor as $k=>$case)
   {
    $Modelhandle = $temp;
    
    if($case['tsuite_name'] != $BeforSuiteNam)
    {
     $Modelhandle = $temp . "<tr><td colspan='3'><b>{$case['tsuite_name']}</b></td></tr>";
     $temp = $Modelhandle;
    }


    //cese name + importance + result
    //var_dump(Integer.parseInt('c'));
    //get success number
    $sql="SELECT TK.id,TK.send_email,TK.user_id,TK.case_ids,TK.testplan_id,TK.flag,TK.total,TK.user_id from tasks AS TK WHERE current=total and flag='2'";
    
    $controlInfor = $db->get_recordset($sql);
    
    //set pass/fail information 
    //var_dump($case['status']);
    $pass = 'p';
    $finish ='c';
    
    if(($case['status'])== $finish)//$TotalCaseNum
    {
     //set case information
     $Modelhandle = $temp. "
      <tr style=\"background-color:red\">
      <td>{$case['name']}</td>
      <td>{$case['importance']}</td>
      <td>FINISH</td>
     </tr>";
    }
    else if($case['status'] == $pass)//$TotalCaseNum
    {
     $successNum++;
          //set case information
     $Modelhandle = $temp. "
      <tr>
      <td>{$case['name']}</td>
      <td>{$case['importance']}</td>
      <td>PASS</td>
     </tr>";
    }
    else
    {
     $Modelhandle = $temp. "
      <tr style=\"background-color:red\">
      <td>{$case['name']}</td>
      <td>{$case['importance']}</td>
      <td>FAIL</td>
      </tr>";
    }
    
    $temp = $Modelhandle;
    $BeforSuiteNam = $case['tsuite_name'];
   }
   
   //set success number
   //var_dump($TotalCaseNum);
   //$TotalCaseNum
   $temp = str_replace("MODEL_SUCCESS","{$successNum}/{$TotalCaseNum}",$Modelhandle);
   $Modelhandle = $temp;
   
   $temp = str_replace("MODEL_FAIL",($TotalCaseNum-$successNum)."/".$TotalCaseNum,$Modelhandle);
   $Modelhandle = $temp;
   
   //added html end sign
   $Modelhandle = $temp . "</table>
        </body>
        </html>";
   
   $fHtml = fopen("report.html","w");
   
   if(fwrite($fHtml,$Modelhandle))
   {
    fclose($fModel);
    fclose($fHtml);
    //die("创建html成功");
   }
   else
   {
    fclose($fModel);
    fclose($fHtml);
   }
   
   //get email address and send email
   $sql="SELECT email,group_email from users WHERE id={$item['user_id']}";
   var_dump($sql);
   
   $result = $db->get_recordset($sql);
   
   if(SendEmail($result[0]['email'],$result[0]['group_email'],$projectName))
   {
    signFinishEmail($db,$item['id']);
   }
   
   $successNum = 0;
  }
  
  //get test case
  return true;
 }
 
 //sign finish send email flag
 function signFinishEmail($db,$TaskId)
 {
  $sql = "UPDATE tasks SET send_email='1' WHERE id='{$TaskId}'";
  //var_dump($sql);
  $db->exec_query($sql);
  return;
 }
 
 //create email conttent
 function CreateHtml()
 {
  $fModel = fopen("report.model","r");
  
  if($fModel)
  {
   $handle = fread($fModel,filesize("report.model"));
   //var_dump($handle);
   $fHtml = fopen("report.html","w");
   
   if(fwrite($fHtml,$handle))
   {
    fclose($fModel);
    fclose($fHtml);
   }
   else
   {
    fclose($fModel);
    fclose($fHtml);
    die("Create html sucess");
   }
  }
  return;
 }
 
  /*
  function: get_ts_name_details

  args :
  
  returns: map with key=TCID
    values= assoc_array([tsuite_id => 5341
         [details] => my detailas ts1
         [tcid] => 5343
         [tsuite_name] => ts1)
 */
 function get_ts_name_details(&$db,$tcase_id)
 {
  $tables = array();
  $termCase = $tcase_id;

  $tables['testsuites'] = DB_TABLE_PREFIX . 'testsuites';
  $tables['nodes_hierarchy'] = DB_TABLE_PREFIX . 'nodes_hierarchy';
  
  $rs = '';
  $do_query = true;
  $sql = "SELECT TS.id AS tsuite_id, TS.details, TCS.status,
     NHA.id AS tc_id, NHA.name,NHB.name AS tsuite_name,TV.importance 
   FROM {$tables['testsuites']} TS, {$tables['nodes_hierarchy']} NHA, 
     {$tables['nodes_hierarchy']} NHB,tcversions TV,executions TCS
   WHERE TS.id=NHA.parent_id
   AND NHB.id=NHA.parent_id
   AND TCS.tcversion_id=NHA.id+1
   AND TV.id=NHA.id+1";

  if( is_array($tcase_id) && count($tcase_id) > 0)
  {
  //note start node
  $tcase_id[count($tcase_id)-1] -= 1;
 
  $in_list = implode("-1,",$tcase_id);
  
  $sql .= " AND NHA.id IN (" . $in_list . ")";
  }
  else if(!is_null($tcase_id))
  {
  $sql .= " AND NHA.id={$tcase_id}";
  }
  else
  {
  $do_query = false;
  }
  if($do_query)
  {
  var_dump($sql);
  $rs = $db->fetchRowsIntoMap($sql,'tc_id');
  }
  
  return $rs;
 }
 
 //send email
 function SendEmail($emailAdress,$groupEmail,$projectName)
 {
  $MailTital="HATP:".$projectName;
  var_dump($groupEmail);
  if($emailAdress=="")
  {
   return false;
  }
  
  if(null !=$groupEmail)
  {
   //var_dump("run here");
   $ReciveEmails = explode(";",$groupEmail);
  }
  
  //process email informaition
  $mail = new PHPMailer(true); //New instance, with exceptions enabled

  $body = file_get_contents('report.html');
  //var_dump($body);
  $body = preg_replace('/\\\\/','', $body); //Strip backslashes

  $mail->IsSMTP(); // tell the class to use SMTP
  $mail->SMTPAuth = true; // enable SMTP authentication
  $mail->Port = 25; // set the SMTP server port
  $mail->Host = "172.20.0.6"; // SMTP server
  $mail->Username = "[email protected]"; // SMTP server username
  $mail->Password = "bugfree"; // SMTP server password
  $mail->From = "testlink";
  $mail->FromName = "testlink";
  $mail->Subject = $MailTital;
  $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
  $mail->WordWrap = 80; // set word wrap
  $mail->MsgHTML($body);
  $mail->IsHTML(true); // send as HTML
  
  //add tester
  $ReciveEmails[sizeof($ReciveEmails)] = $emailAdress;
  var_dump($ReciveEmails);
  foreach($ReciveEmails as $K=>$Item)
  {
   var_dump($Item);
   $mail->AddAddress($Item);//to send man
  }
  
  try 
  {
   $mail->Send();
  }
  catch (phpmailerException $e) 
  {
   echo $e->errorMessage();
   return false;
  }
  
  $ReciveEmails='';
  return TRUE;
 }

Tags:PHP 后台 分钟

编辑录入:coldstar [复制链接] [打 印]
赞助商链接