您好,欢迎访问一九零五行业门户网

php监控日志500、503错误并发送邮件提示的代码

issmtp(); // telling the class to use smtp$mail->smtpdebug = 1; // enables smtp debug information (for testing)// 1 = errors and messages// 2 = messages only$mail->smtpauth = true; // enable smtp authentication$mail->host = smtp.126.com; // sets the smtp server$mail->port = 25; // set the smtp port for the gmail server$mail->username = xxx@126.com; // smtp account username 邮箱用户名$mail->password = xxxxxxxxx; // smtp account password 密码$mail->setfrom('xxxxxxx@126.com', '报错');//$now = u_ex.date(ymdh)..log;
try{
$log = file(f:/iis-log/w3svc2/.$now);//日志路径自己改}catch(exception $e){ echo no file ; exit; }$error = array();
$start = date(i,time()-120);$end = date(i);if($start > $end){ exit;}$aa = range($start,$end);$code = mkstr($aa);echo $code;$pattern = /($code).*(\s500\s|\s503\s)/;//这里自己修改$i = 0;foreach($log as $k => $v){ if(preg_match($pattern,$v)){ $error[] = $v; echo $v; $i += 1; }}if(!empty($error)){ $content = implode(
,$error); $mail->subject=$i errors ; $mail->addaddress(xxxxxxxxxx@163.com); $mail->addaddress(xxxxxxxx@126.com); $mail->msghtml($content); $mail->send();}echo \nover;function mkstr($arr){ $code = ''; foreach($arr as $k => $e){ $code .=\:$e\:|; } $code = rtrim($code,|); return $code;}
复制代码
其它类似信息

推荐信息