首先,你先把该做的都做了,异常处理接管
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);
} 最佳答案