1.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 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['importance']}
finish
;
}
else if($case['status'] == $pass)//$totalcasenum
{
$successnum++;
//set case information
$modelhandle = $temp.
{$case['name']}
{$case['importance']}
pass
;
}
else
{
$modelhandle = $temp.
{$case['name']}
{$case['importance']}
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成功);
}
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 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;
}