搜索
系统检测到您的用户名不符合规范:

系统报错,就给你发邮件(可以扩展发送短信)

浏览:1268 发布日期:2016年12月16日 分类:ThinkPHP5专区 关键字: exception
大家都知道,一个牛X的人开发者,总能第一时间处理bug,那么当你上线之后回家睡大觉了,半夜系统出问题,谁来第一时间通知你呢?一般情况下,我们都是通过短信或者邮件来通知我们,今天就来给大家说一下,通过ThinkPHP5的异常处理接管来实现这个需求。

首先,你先把该做的都做了,异常处理接管
http://www.kancloud.cn/manual/thinkphp5/126075

然后,将一下代码覆盖到 Http.php 这个文件namespace app\common\exception;


use app\common\helper\MailHelper;
use think\exception\Handle;

class Http extends Handle
{
/**
* 接管异常
* @param \Exception $e
* @return \think\Response
*/
public function render(\Exception $e)
{
MailHelper::errorSend('出错了~', self::getErrorHtml($e));
return parent::render($e);
}

/**
* 生成异常html
* @param $e
* @return string
*/
protected static function getErrorHtml($e)
{
$html = '<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<table style="width: 800px; margin: 0 auto; border: 1px solid #ccc;">
<thead>
<tr style="background: #ff0000; color: #ffffff; height: 50px;">
<th colspan="2">Error Info:</th>
</tr>
</thead>
<tbody>
<tr>
<td style="height: 50px; width: 100px; text-align: center; background: #ccc">message</td>
<td style="height: 50px;">'. $e->getMessage() .'</td>
</tr>
<tr>
<td style="height: 50px; width: 100px; text-align: center; background: #ccc">file</td>
<td style="height: 50px;">'. $e->getFile() .'</td>
</tr>
<tr>
<td style="height: 50px; width: 100px; text-align: center; background: #ccc">line</td>
<td style="height: 50px;">'. $e->getLine() .'</td>
</tr>
<tr>
<td style="height: 50px; width: 100px; text-align: center; background: #ccc">trace</td>
<td style="height: 50px;">'. $e->getTraceAsString() .'</td>
</tr>
</tbody>
</table>
</body>
</html>
';
return $html;

}
}
继续,执行 composer require nette/mail

新建 MailHelper.php文件,请根据命名空间创建到对应的目录namespace app\common\helper;


use Nette\Mail\Message;
use Nette\Mail\SmtpMailer;

class MailHelper
{
/**
* 邮件发送:默认系统通知
* @param string $title 邮件标题
* @param string $content 邮件内容
* @param array $email email 支持多个
*/
public static function send($title = '', $content = '', $email = [], $config = [])
{
$config = $config ? : config('mail.no-reply');
$mail = new Message();
$mail->setFrom("{$config['showname']} <{$config['username']}>")
->setSubject($title)
->setHtmlBody($content);
foreach ($email as $value) {
$mail->addTo($value);
}
$mailer = new SmtpMailer($config);
$mailer->send($mail);
}

/**
* 邮件发送:系统异常
* @param string $title
* @param string $content
* @param array $email
*/
public static function errorSend($title = '', $content = '', $email = [])
{
$config = config('mail.system-error');
$email = $email ? : config('mail.email');
self::send($title, $content, $email, $config);
}
}
再继续,在配置文件中添加邮箱配置,这是我的配置,具体自己的业务可以自己配置// 邮件配置
'mail' => [
'no-reply' => [
'showname' => 'xx网站',
'host' => 'smtp.****.com',
'username' => 'no-reply@abc.com',
'password' => 'NR2016@@@',
'secure' => ''
],
'system-error' => [
'showname' => 'xx网站异常',
'host' => 'smtp.****.com',
'username' => 'system-error@abc.com',
'password' => 'SE2016@@@',
'secure' => ''
],
'email' => [ // 异常发送对象
'shenfakuan@163.com',
]
],
好了,我要说的只有这么多了,看下效果图吧



当然,你会发现,404也会发送邮件,有些人没事就天天测试你的url,那我们就把404给屏蔽了吧/**
* 接管异常
* @param \Exception $e
* @return \think\Response
*/
public function render(\Exception $e)
{
$statusCode = null;
if ($e instanceof HttpException) {
$statusCode = $e->getStatusCode();
}

$send_mail = false;

if ($statusCode != null && $statusCode != 404) {
$send_mail = true;
}

if ($statusCode == null) {
$send_mail = true;
}

if ($send_mail) {
MailHelper::errorSend('出错了~', self::getErrorHtml($e));
}

return parent::render($e);
}
最佳答案
评论() 相关
后面还有条评论,
评论支持使用[code][/code]标签添加代码
您需要登录后才可以评论 登录 | 立即注册
收藏
shenfakuan
积分:1540 等级:LV3
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /