PHP backstage 5 minutes to send email email

Source: Internet
Author: User
Tags foreach count end explode fread sql word wrap server port

1.5 minutes to send email, and the message content is generated by the HTML Template 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 Task S 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 ("Create HTML Success");


      }


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 = "bugfree@hojy.com"; SMTP Server username


$mail->password = "Bugfree"; SMTP Server Password


$mail->from = "Testlink";


$mail->fromname = "Testlink";


$mail->subject = $MailTital;


$mail->altbody = "To view", 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;


  }







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.