搜尋
首頁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、到此差不多了,測試一下,隨便寫點報錯,然後請求接口看是否會為您設定的郵件信箱會傳送郵件,結果圖如下:

Laravel 郵件推播系統異常

#

以上是Laravel 郵件推播系統異常的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:learnku。如有侵權,請聯絡admin@php.cn刪除
最新的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

是的。

Laravel:軟刪除了性能問題Laravel:軟刪除了性能問題May 12, 2025 am 12:04 AM

SoftDeletesinLaravelimpactperformancebycomplicatingqueriesandincreasingstorageneeds.Tomitigatetheseissues:1)Indexthedeleted_atcolumntospeedupqueries,2)Useeagerloadingtoreducequerycount,and3)Regularlycleanupsoft-deletedrecordstomaintaindatabaseefficie

Laravel遷移對什麼有益?用例和福利Laravel遷移對什麼有益?用例和福利May 11, 2025 am 12:14 AM

Laravelmigrationsarebeneficialforversioncontrol,collaboration,andpromotinggooddevelopmentpractices.1)Theyallowtrackingandrollingbackdatabasechanges.2)Migrationsensureteammembers'schemasstaysynchronized.3)Theyencouragethoughtfuldatabasedesignandeasyre

如何在Laravel中使用軟刪除:保護您的數據如何在Laravel中使用軟刪除:保護您的數據May 11, 2025 am 12:14 AM

Laravel的軟刪除功能通過標記記錄而非實際刪除來保護數據。 1)在模型中添加SoftDeletestrait和deleted_at字段。 2)使用delete()方法標記刪除,使用restore()方法恢復。 3)查詢時使用withTrashed()或onlyTrashed()包含軟刪除記錄。 4)定期清理超過一定時間的軟刪除記錄以優化性能。

Laravel遷移是什麼,您如何使用它們?Laravel遷移是什麼,您如何使用它們?May 11, 2025 am 12:13 AM

laravelmigrationSareversionControlfordatabaseschemas,允許Roducibleandreversiblechanges.tousethem:1)creatighatsanmake:遷移',2)定義chemachangesinthe'up()

Laravel遷移:回滾行不通,發生了什麼事?Laravel遷移:回滾行不通,發生了什麼事?May 11, 2025 am 12:10 AM

Laravelmigrationsmayfailtorollbackduetodataintegrityissues,foreignkeyconstraints,orirreversibleactions.1)Dataintegrityissuescanoccurifamigrationaddsdatathatcan'tbeundone,likeacolumnwithadefaultvalue.2)Foreignkeyconstraintscanpreventrollbacksifrelatio

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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具