The backend sends an email every five minutes. The email content is html.
The email is sent in 1.5 minutes, and the email content is generated by the table generated by the html template. 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 _ id']; // 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] ['note'], $ 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 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 ." |
{$ Case ['tsuite _ name']} |
";$ 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 ."
{$ Case ['name']} |
{$ Case ['portance ']} |
FINISH |
";}Else if ($ case ['status'] = $ pass) // $ TotalCaseNum{$ SuccessNum ++;// Set case information$ Modelhandle = $ temp ."
{$ Case ['name']} |
{$ Case ['portance ']} |
PASS |
";}Else{$ Modelhandle = $ temp ."
{$ Case ['name']} |
{$ Case ['portance ']} |
FAIL |
";} $ 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 ."
";
$ FHtml = fopen ("report.html", "w ");
If (fwrite ($ fHtml, $ Modelhandle ))
{
Fclose ($ fModel );
Fclose ($ fHtml );
// Die ("html created successfully ");
}
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 = & gt; 5341
[Details] => my detailas ts1
[Tcid] = & gt; 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 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;
}