搜索
首页php框架LaravelLaravel 邮件推送系统异常

Laravel 邮件推送系统异常

Nov 04, 2019 pm 01:27 PM
laravel

Laravel 邮件推送系统异常

前言:

在日常开发中,我们可能会写出一些 bug,但是项目已经上线了,我们无法第一时间得到客户的反馈,这时候邮件报错系统就派上用场了,我们都知道 laravel 的异常处理都需要通过 Handler.php 文件来处理,那么就从此处开始入手

1、在 report 的方法里判断是否需要发送邮件,代码如下:

public function report(Exception $exception)
{
    //判断是否需要发送邮件
    if (config('mail.mailException.report')) {
        $this->mailReport($exception, config('mail.mailException.toAccounts', []));
    }
    parent::report($exception);
}

2、定义一个发送邮件的方法,代码如下:

/**
 * 邮件通知错误报告.
 *
 * @param $exception object 错误信息
 * @param array $accounts array 收件人
 */
public function mailReport($exception, $accounts = [])
{
    if (!empty($accounts)) {
        try {
            $e       = FlattenException::create($exception);
            $handler = new SymfonyExceptionHandler();
            $html    = $handler->getHtml($e);
            $mail    = new Mail();
            $mail::to($accounts)->send(new ExceptionReport($html));
        } catch (Exception $ex) {
        }
    }
}

3、新建一个邮件异常类,放在 App 下的 Mail 文件夹中,名为:ExceptionReport.php,代码如下:

<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ExceptionReport extends Mailable
{
    use Queueable, SerializesModels;
    /**
     * Create a new message instance.
     */
    public $subject = &#39;错误报告通知&#39;; //邮件标题
    private $exception; 
    //通过构造函数实例化异常
    public function __construct($exception)
    {
        $this->exception = $exception;
    }
    /**
     * build一个异常发送模板
     *
     * @return $this
     */
    public function build()
    {
        return $this->view(&#39;mail.exception&#39;)->with([&#39;content&#39; => $this->exception]);
    }
}

4、新建一个异常发送的模板视图,在 View 下新建一个 mail 文件夹,创建名为:exception.blade.php 的视图模板文件,代码如下:

{!! $content !!}

5、在 config 下配置需要发送邮件的人员和是否发送的开关,在最末尾加上以下代码:

  &#39;mailException&#39; => [
        &#39;report&#39; => env(&#39;MailExceptionReport&#39;, 0),//是否邮件通知错误报告
        &#39;toAccounts&#39; => [
            &#39;guifeng.liang@zun1.com&#39;,
        ]
    ] ,

6、在.env 文件中配置好邮箱发送系统的信息,格式如下:

 //邮件驱动
 MAIL_DRIVER=smtp
 //邮件服务器
 MAIL_HOST=smtp.exmail.qq.com
 //邮件端口
 MAIL_PORT=465
 //邮箱用户名
 MAIL_USERNAME=1231231@qq.com
 //邮箱密码
MAIL_PASSWORD=dasdasdadasdad
//加密方式
MAIL_ENCRYPTION=ssl
//发件人邮箱
MAIL_FROM_ADDRESS=1476982312@qq.com
//发件人姓名
MAIL_FROM_NAME=犯二青年
//是否发送邮件,发送为1,不发送为0
MailExceptionReport=1

7、到此差不多了,测试一下,随便写点报错,然后请求接口看是否会为您配置的邮箱发送邮件,结果图如下:

3177c2b554a20866bac2d71cba447cb.png

以上是Laravel 邮件推送系统异常的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:learnku。如有侵权,请联系admin@php.cn删除
如何使用Laravel迁移:逐步教程如何使用Laravel迁移:逐步教程May 13, 2025 am 12:15 AM

laravelmigrationsStreamLinedAtabasemangementbyallowingbolAlyChemachangeStobEdeDinedInphpcode,whobeversion-controllolleDandShared.here'showtousethem:1)createMigrationClassestodeFinePerationFineFineOperationsLikeCreatingingModifyingTables.2)

查找最新的Laravel版本:快速简便的指南查找最新的Laravel版本:快速简便的指南May 13, 2025 am 12:13 AM

要查找最新版本的Laravel,可以访问官方网站laravel.com并点击右上角的"Docs"按钮,或使用Composer命令"composershowlaravel/framework|grepversions"。保持更新有助于提升项目安全性和性能,但需考虑对现有项目的影响。

使用Laravel的更新:使用最新版本的好处使用Laravel的更新:使用最新版本的好处May 13, 2025 am 12:08 AM

youshouldupdateTotheLateStlaravelVerverSionForPerformanceImprovements,增强的安全性,newfeatures,BetterCommunitySupport,and long-term-Maintenance.1)绩效:Laravel9'Selover9'seloquentormoptimizatizationenenhanceApplicationsPeed.2)secuse:laravel8InIntrododeDodecter.2)

Laravel:我搞砸了我的迁移,我该怎么办?Laravel:我搞砸了我的迁移,我该怎么办?May 13, 2025 am 12:06 AM

当您的inoumessupamigrationInlaravel,youcan:1)滚动播放'phpartisanmigrate:rollback'ifit'Sthelastone,or'phpartisanmigrate:reset'forall; 2)crecteAneAnewmigrateTocorrectRateRectRectRateRectRectRectRectRectRectRectRectErcrationInproduction; 3)

最后的Laravel版本:性能指南最后的Laravel版本:性能指南May 13, 2025 am 12:04 AM

toboostPerformanceInthelateStlaravelversion,关注详细信息:1)用户disforcachingtoimproveresponsetimes和Reddiccistatabaseload.2)优化的AtabasequesquesquesquesquesquesquesquesqueriesWitheAgerloadingTopreventn 1Queryissues.3)emplortecachingInprodododododododoductuproutroutrououtrououToreSourte。

最新的Laravel版本:DIFES DISCON最新的Laravel版本:DIFES DISCONMay 12, 2025 am 12:15 AM

Laravel10IntroducessEveralKeyFeatUrestHatenHanceWebDevelopment.1)LazyCollectionsAllyCollefficeProcesingOflargeFlargedAtasetSwithSwithOutloadingAllRecordSintomeMemory.2)the Make:Model Model Moged-and-Mogration'ArtisanCommandSancancMommandSimplififieScreatingModeltigation.3)

Laravel迁移解释了:创建,修改和管理您的数据库Laravel迁移解释了:创建,修改和管理您的数据库May 12, 2025 am 12:11 AM

laravelmigrationssshouldbeusedbecausetheystreamlinedeplupment,nesurecresistencyAcrossenviments和simplifyCollaborationAndDeployment.1)shemallogragrammatonofdatabaseschemachanges,ReeducingErrors.2)MigrigationScanBeverCanbeverSionConconconconcontrollin.2)

Laravel迁移:值得使用吗?Laravel迁移:值得使用吗?May 12, 2025 am 12:10 AM

是的。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。