|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
小试一下身手,大概是没问题了,那么交给你个任务,做个留言本吧,这和HELLOWORLD有一比啊!^_^,同是新手面临的第一道关。
1.5分钟发送email,而且邮件内容为由html模板天生的table
while(1)
{
//tenminute
var_dump("checktask,pleasedontclose");
//sendemail
processDBData($db);
//processinventorystate
processInventoryState($db,$tasks);
sleep(5*60);
}
//readtabledatafromdatabase
functionprocessDBData($db)
{
$testplan_id=;
$temp=;
$TotalCaseNum=0;
$projectName=;
//checkfinishedtestplantable->tasks
$sql="SELECTTK.id,TK.send_email,TK.user_id,TK.build_id,TK.case_ids,TK.testplan_id,TK.flag,TK.total
fromtasksASTKWHEREcurrent=totalandflag=2andISNULL(send_email)";
$controlInfor=$db->get_recordset($sql);
if(null==$controlInfor)
{
return;
}
foreach($controlInforas$key=>$item)
{
//openmodelhtml
$fModel=fopen("report.model","r");
if(null==$fModel)
{
var_dump("nomodel");
}
$Modelhandle=fread($fModel,filesize("report.model"));
$test_cases=$item[case_ids];
//gettotalNum
$TotalCaseNum=(int)$item[total];
//testplantable->testplan
//$sql="SELECTnotes,testproject_idFROMtestplansWHEREid={$item[testplan_id]}";
$sql="SELECTnamefromnodes_hierarchyWHEREid={$item[testplan_id]}";
$result=$db->get_recordset($sql);
$temp=str_replace("MODEL_TESLPLANE",$result[0][name],$Modelhandle);
$Modelhandle=$temp;
//producttable->testprojects
$sql="SELECTnotesFROMtestprojectsWHEREid={$result[0][testproject_id]}";
var_dump($sql);
$result=$db->get_recordset($sql);
$temp=str_replace("MODEL_PRODUCTNAME",$result[0][notes],$Modelhandle);
$Modelhandle=$temp;
//buildtable->builds
$sql="SELECTDB.nameFROMbuildsASDBWHEREtestplan_id={$item[testplan_id]}andid={$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;
//testresulttable->executions
$cases=explode(,,$test_cases);
$case_infor=get_ts_name_details($db,$cases);
//var_dump(sizeof($case_infor));
//processhtmltestcasecontent<!--3.testcasecontent-->nl2br()
//afterthecontentadded(table,body,htmlendflag)
var_dump($case_infor);
$BeforSuiteNam="";
$successNum=0;
$TESTNUM=0;
//var_dump($case_infor);
//var_dump($case_infor);
foreach($case_inforas$k=>$case)
{
$Modelhandle=$temp;
if($case[tsuite_name]!=$BeforSuiteNam)
{
$Modelhandle=$temp."<tr><tdcolspan=3><b>{$case[tsuite_name]}</b></td></tr>";
$temp=$Modelhandle;
}
//cesename+importance+result
//var_dump(Integer.parseInt(c));
//getsuccessnumber
$sql="SELECTTK.id,TK.send_email,TK.user_id,TK.case_ids,TK.testplan_id,TK.flag,TK.total,TK.user_idfromtasksASTKWHEREcurrent=totalandflag=2";
$controlInfor=$db->get_recordset($sql);
//setpass/failinformation
//var_dump($case[status]);
$pass=p;
$finish=c;
if(($case[status])==$finish)//$TotalCaseNum
{
//setcaseinformation
$Modelhandle=$temp."
<trstyle="background-color:red">
<td>{$case[name]}</td>
<td>{$case[importance]}</td>
<td>FINISH</td>
</tr>";
}
elseif($case[status]==$pass)//$TotalCaseNum
{
$successNum++;
//setcaseinformation
$Modelhandle=$temp."
<tr>
<td>{$case[name]}</td>
<td>{$case[importance]}</td>
<td>PASS</td>
</tr>";
}
else
{
$Modelhandle=$temp."
<trstyle="background-color:red">
<td>{$case[name]}</td>
<td>{$case[importance]}</td>
<td>FAIL</td>
</tr>";
}
$temp=$Modelhandle;
$BeforSuiteNam=$case[tsuite_name];
}
//setsuccessnumber
//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;
//addedhtmlendsign
$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);
}
//getemailaddressandsendemail
$sql="SELECTemail,group_emailfromusersWHEREid={$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;
}
//gettestcase
returntrue;
}
//signfinishsendemailflag
functionsignFinishEmail($db,$TaskId)
{
$sql="UPDATEtasksSETsend_email=1WHEREid={$TaskId}";
//var_dump($sql);
$db->exec_query($sql);
return;
}
//createemailconttent
functionCreateHtml()
{
$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("Createhtmlsucess");
}
}
return;
}
/*
function:get_ts_name_details
args:
returns:mapwithkey=TCID
values=assoc_array([tsuite_id=>5341
[details]=>mydetailasts1
[tcid]=>5343
[tsuite_name]=>ts1)
*/
functionget_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="SELECTTS.idAStsuite_id,TS.details,TCS.status,
NHA.idAStc_id,NHA.name,NHB.nameAStsuite_name,TV.importance
FROM{$tables[testsuites]}TS,{$tables[nodes_hierarchy]}NHA,
{$tables[nodes_hierarchy]}NHB,tcversionsTV,executionsTCS
WHERETS.id=NHA.parent_id
ANDNHB.id=NHA.parent_id
ANDTCS.tcversion_id=NHA.id+1
ANDTV.id=NHA.id+1";
if(is_array($tcase_id)&&count($tcase_id)>0)
{
//notestartnode
$tcase_id[count($tcase_id)-1]-=1;
$in_list=implode("-1,",$tcase_id);
$sql.="ANDNHA.idIN(".$in_list.")";
}
elseif(!is_null($tcase_id))
{
$sql.="ANDNHA.id={$tcase_id}";
}
else
{
$do_query=false;
}
if($do_query)
{
var_dump($sql);
$rs=$db->fetchRowsIntoMap($sql,tc_id);
}
return$rs;
}
//sendemail
functionSendEmail($emailAdress,$groupEmail,$projectName)
{
$MailTital="HATP:".$projectName;
var_dump($groupEmail);
if($emailAdress=="")
{
returnfalse;
}
if(null!=$groupEmail)
{
//var_dump("runhere");
$ReciveEmails=explode(";",$groupEmail);
}
//processemailinformaition
$mail=newPHPMailer(true);//Newinstance,withexceptionsenabled
$body=file_get_contents(report.html);
//var_dump($body);
$body=preg_replace(/\/,,$body);//Stripbackslashes
$mail->IsSMTP();//telltheclasstouseSMTP
$mail->SMTPAuth=true;//enableSMTPauthentication
$mail->Port=25;//settheSMTPserverport
$mail->Host="172.20.0.6";//SMTPserver
$mail->Username="bugfree@hojy.com";//SMTPserverusername
$mail->Password="bugfree";//SMTPserverpassword
$mail->From="testlink";
$mail->FromName="testlink";
$mail->Subject=$MailTital;
$mail->AltBody="Toviewthemessage,pleaseuseanHTMLcompatibleemailviewer!";//optional,commentoutandtest
$mail->WordWrap=80;//setwordwrap
$mail->MsgHTML($body);
$mail->IsHTML(true);//sendasHTML
//addtester
$ReciveEmails[sizeof($ReciveEmails)]=$emailAdress;
var_dump($ReciveEmails);
foreach($ReciveEmailsas$K=>$Item)
{
var_dump($Item);
$mail->AddAddress($Item);//tosendman
}
try
{
$mail->Send();
}
catch(phpmailerException$e)
{
echo$e->errorMessage();
returnfalse;
}
$ReciveEmails=;
returnTRUE;
}
不断巩固,摸透大部分PHP常用函数,并可理解OOP,MYSQL优化,以及模板 |
|