[{"data":1,"prerenderedAt":747},["ShallowReactive",2],{"article:\u002Fblog\u002Fexception-reporting-email-for-laravel-8":3},{"article":4,"surroundings":738},{"id":5,"title":6,"body":7,"description":728,"extension":729,"meta":730,"navigation":150,"path":731,"published":732,"seo":733,"sitemap":734,"stem":735,"tag":736,"__hash__":737},"blog\u002Fblog\u002Fexception-reporting-email-for-laravel-8.md","Exception Reporting Email for Laravel 8",{"type":8,"value":9,"toc":726},"minimark",[10,14,22,32,38,44,47,53,64,97,103,106,129,136,336,342,345,369,376,453,460,465,719,722],[11,12,13],"p",{},"This tutorial teaches how to send an email to report when an error exception happens.",[11,15,16,17,21],{},"First, to save the email addresses that are going to receive the error exception reports, add this line to your ",[18,19,20],"code",{},".env.example"," file.",[23,24,29],"pre",{"className":25,"code":27,"language":28},[26],"language-text","ERROR_REPORTING_EMAIL=\n","text",[18,30,27],{"__ignoreMap":31},"",[11,33,34,35,21],{},"And this line to your ",[18,36,37],{},".env",[23,39,42],{"className":40,"code":41,"language":28},[26],"ERROR_REPORTING_EMAIL=youremail@example.com,yoursecondemail@example.com\n",[18,43,41],{"__ignoreMap":31},[11,45,46],{},"Next, to create a mail job, we run this command.",[23,48,51],{"className":49,"code":50,"language":28},[26],"php artisan make:mail ExceptionOccurred\n",[18,52,50],{"__ignoreMap":31},[11,54,55,56,59,60,63],{},"In the ",[18,57,58],{},"app\u002FMail\u002FExceptionOccurred.php"," file, we need to add a public variable ",[18,61,62],{},"$content",", and save the content into this variable in the constructor. The constructor now looks like this:",[23,65,69],{"className":66,"code":67,"language":68,"meta":31,"style":31},"language-PHP shiki shiki-themes github-light github-dark","public function __construct($content)\n{\n    $this->content = $content;\n}\n","PHP",[18,70,71,79,85,91],{"__ignoreMap":31},[72,73,76],"span",{"class":74,"line":75},"line",1,[72,77,78],{},"public function __construct($content)\n",[72,80,82],{"class":74,"line":81},2,[72,83,84],{},"{\n",[72,86,88],{"class":74,"line":87},3,[72,89,90],{},"    $this->content = $content;\n",[72,92,94],{"class":74,"line":93},4,[72,95,96],{},"}\n",[11,98,99,100,102],{},"Notice that the constructor takes in a ",[18,101,62],{}," argument.",[11,104,105],{},"In the build method, we return a blade view with the email content.",[23,107,109],{"className":66,"code":108,"language":68,"meta":31,"style":31},"public function build()\n{\n    return $this->view('emails.exception')->with('content', $this->content);\n}\n",[18,110,111,116,120,125],{"__ignoreMap":31},[72,112,113],{"class":74,"line":75},[72,114,115],{},"public function build()\n",[72,117,118],{"class":74,"line":81},[72,119,84],{},[72,121,122],{"class":74,"line":87},[72,123,124],{},"    return $this->view('emails.exception')->with('content', $this->content);\n",[72,126,127],{"class":74,"line":93},[72,128,96],{},[11,130,131,132,135],{},"The whole ",[18,133,134],{},"ExceptionOccurred.php"," file now looks like this:",[23,137,139],{"className":66,"code":138,"language":68,"meta":31,"style":31},"\u003C?php\n\nnamespace App\\Mail;\n\nuse Illuminate\\Bus\\Queueable;\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\nuse Illuminate\\Mail\\Mailable;\nuse Illuminate\\Queue\\SerializesModels;\n\nclass ExceptionOccurred extends Mailable\n{\n    use Queueable, SerializesModels;\n\n    public $content;\n\n    \u002F**\n     * Create a new message instance.\n     *\n     * @return void\n     *\u002F\n    public function __construct($content)\n    {\n        $this->content = $content;\n    }\n\n    \u002F**\n     * Build the message.\n     *\n     * @return $this\n     *\u002F\n    public function build()\n    {\n        return $this->view('emails.exception')->with('content', $this->content);\n    }\n}\n",[18,140,141,146,152,157,161,167,173,179,185,190,196,201,207,212,218,223,229,235,241,247,253,259,265,271,277,282,287,293,298,304,309,315,320,326,331],{"__ignoreMap":31},[72,142,143],{"class":74,"line":75},[72,144,145],{},"\u003C?php\n",[72,147,148],{"class":74,"line":81},[72,149,151],{"emptyLinePlaceholder":150},true,"\n",[72,153,154],{"class":74,"line":87},[72,155,156],{},"namespace App\\Mail;\n",[72,158,159],{"class":74,"line":93},[72,160,151],{"emptyLinePlaceholder":150},[72,162,164],{"class":74,"line":163},5,[72,165,166],{},"use Illuminate\\Bus\\Queueable;\n",[72,168,170],{"class":74,"line":169},6,[72,171,172],{},"use Illuminate\\Contracts\\Queue\\ShouldQueue;\n",[72,174,176],{"class":74,"line":175},7,[72,177,178],{},"use Illuminate\\Mail\\Mailable;\n",[72,180,182],{"class":74,"line":181},8,[72,183,184],{},"use Illuminate\\Queue\\SerializesModels;\n",[72,186,188],{"class":74,"line":187},9,[72,189,151],{"emptyLinePlaceholder":150},[72,191,193],{"class":74,"line":192},10,[72,194,195],{},"class ExceptionOccurred extends Mailable\n",[72,197,199],{"class":74,"line":198},11,[72,200,84],{},[72,202,204],{"class":74,"line":203},12,[72,205,206],{},"    use Queueable, SerializesModels;\n",[72,208,210],{"class":74,"line":209},13,[72,211,151],{"emptyLinePlaceholder":150},[72,213,215],{"class":74,"line":214},14,[72,216,217],{},"    public $content;\n",[72,219,221],{"class":74,"line":220},15,[72,222,151],{"emptyLinePlaceholder":150},[72,224,226],{"class":74,"line":225},16,[72,227,228],{},"    \u002F**\n",[72,230,232],{"class":74,"line":231},17,[72,233,234],{},"     * Create a new message instance.\n",[72,236,238],{"class":74,"line":237},18,[72,239,240],{},"     *\n",[72,242,244],{"class":74,"line":243},19,[72,245,246],{},"     * @return void\n",[72,248,250],{"class":74,"line":249},20,[72,251,252],{},"     *\u002F\n",[72,254,256],{"class":74,"line":255},21,[72,257,258],{},"    public function __construct($content)\n",[72,260,262],{"class":74,"line":261},22,[72,263,264],{},"    {\n",[72,266,268],{"class":74,"line":267},23,[72,269,270],{},"        $this->content = $content;\n",[72,272,274],{"class":74,"line":273},24,[72,275,276],{},"    }\n",[72,278,280],{"class":74,"line":279},25,[72,281,151],{"emptyLinePlaceholder":150},[72,283,285],{"class":74,"line":284},26,[72,286,228],{},[72,288,290],{"class":74,"line":289},27,[72,291,292],{},"     * Build the message.\n",[72,294,296],{"class":74,"line":295},28,[72,297,240],{},[72,299,301],{"class":74,"line":300},29,[72,302,303],{},"     * @return $this\n",[72,305,307],{"class":74,"line":306},30,[72,308,252],{},[72,310,312],{"class":74,"line":311},31,[72,313,314],{},"    public function build()\n",[72,316,318],{"class":74,"line":317},32,[72,319,264],{},[72,321,323],{"class":74,"line":322},33,[72,324,325],{},"        return $this->view('emails.exception')->with('content', $this->content);\n",[72,327,329],{"class":74,"line":328},34,[72,330,276],{},[72,332,334],{"class":74,"line":333},35,[72,335,96],{},[11,337,338,339],{},"Next, create a blade view file in this location: ",[18,340,341],{},"resources\u002Fviews\u002Femails\u002Fexception.blade.php",[11,343,344],{},"This blade file is the content that will be in the email. You can parse the exception message here.",[23,346,348],{"className":66,"code":347,"language":68,"meta":31,"style":31},"Your Site: Error Exception\n\u003Cbr>\n\u003Cbr>\n{!! $content !!}\n",[18,349,350,355,360,364],{"__ignoreMap":31},[72,351,352],{"class":74,"line":75},[72,353,354],{},"Your Site: Error Exception\n",[72,356,357],{"class":74,"line":81},[72,358,359],{},"\u003Cbr>\n",[72,361,362],{"class":74,"line":87},[72,363,359],{},[72,365,366],{"class":74,"line":93},[72,367,368],{},"{!! $content !!}\n",[11,370,371,372,375],{},"Finally, in the ",[18,373,374],{},"app\u002FExceptions\u002FHandler.php"," file, in the register method, add the send mail function to the handlers.",[23,377,379],{"className":66,"code":378,"language":68,"meta":31,"style":31},"public function register()\n{\n    $this->reportable(function (Throwable $e) {\n        if ($this->shouldReport($e)) {\n            $to = explode(',', env('ERROR_REPORTING_EMAIL', ''));\n            Mail::to($to)->send(new ExceptionOccurred($e));\n        }\n    });\n\n    $this->renderable(function (Throwable $e) {\n        if ($this->shouldReport($e)) {\n            $to = explode(',', env('ERROR_REPORTING_EMAIL', ''));\n            Mail::to($to)->send(new ExceptionOccurred($e));\n        }\n    });\n}\n",[18,380,381,386,390,395,400,405,410,415,420,424,429,433,437,441,445,449],{"__ignoreMap":31},[72,382,383],{"class":74,"line":75},[72,384,385],{},"public function register()\n",[72,387,388],{"class":74,"line":81},[72,389,84],{},[72,391,392],{"class":74,"line":87},[72,393,394],{},"    $this->reportable(function (Throwable $e) {\n",[72,396,397],{"class":74,"line":93},[72,398,399],{},"        if ($this->shouldReport($e)) {\n",[72,401,402],{"class":74,"line":163},[72,403,404],{},"            $to = explode(',', env('ERROR_REPORTING_EMAIL', ''));\n",[72,406,407],{"class":74,"line":169},[72,408,409],{},"            Mail::to($to)->send(new ExceptionOccurred($e));\n",[72,411,412],{"class":74,"line":175},[72,413,414],{},"        }\n",[72,416,417],{"class":74,"line":181},[72,418,419],{},"    });\n",[72,421,422],{"class":74,"line":187},[72,423,151],{"emptyLinePlaceholder":150},[72,425,426],{"class":74,"line":192},[72,427,428],{},"    $this->renderable(function (Throwable $e) {\n",[72,430,431],{"class":74,"line":198},[72,432,399],{},[72,434,435],{"class":74,"line":203},[72,436,404],{},[72,438,439],{"class":74,"line":209},[72,440,409],{},[72,442,443],{"class":74,"line":214},[72,444,414],{},[72,446,447],{"class":74,"line":220},[72,448,419],{},[72,450,451],{"class":74,"line":225},[72,452,96],{},[11,454,455,456,459],{},"If there are exception types that you do not wish to report, you can add them to the ",[18,457,458],{},"$dontReport"," array.",[11,461,131,462,135],{},[18,463,464],{},"Handler.php",[23,466,468],{"className":66,"code":467,"language":68,"meta":31,"style":31},"\u003C?php\n\nnamespace App\\Exceptions;\n\nuse Illuminate\\Foundation\\Exceptions\\Handler as ExceptionHandler;\nuse Throwable;\nuse Mail;\nuse App\\Mail\\ExceptionOccurred;\n\nclass Handler extends ExceptionHandler\n{\n    \u002F**\n     * A list of the exception types that are not reported.\n     *\n     * @var array\n     *\u002F\n    protected $dontReport = [\n        \u002F\u002F\n    ];\n\n    \u002F**\n     * A list of the inputs that are never flashed for validation exceptions.\n     *\n     * @var array\n     *\u002F\n    protected $dontFlash = [\n        'password',\n        'password_confirmation',\n    ];\n\n    \u002F**\n     * Register the exception handling callbacks for the application.\n     *\n     * @return void\n     *\u002F\n    public function register()\n    {\n        $this->reportable(function (Throwable $e) {\n            if ($this->shouldReport($e)) {\n                $to = explode(',', env('ERROR_REPORTING_EMAIL', ''));\n                Mail::to($to)->send(new ExceptionOccurred($e));\n            }\n        });\n\n        $this->renderable(function (Throwable $e) {\n            if ($this->shouldReport($e)) {\n                $to = explode(',', env('ERROR_REPORTING_EMAIL', ''));\n                Mail::to($to)->send(new ExceptionOccurred($e));\n            }\n        });\n    }\n}\n",[18,469,470,474,478,483,487,492,497,502,507,511,516,520,524,529,533,538,542,547,552,557,561,565,570,574,578,582,587,592,597,601,605,609,614,618,622,626,632,637,643,649,655,661,667,673,678,684,689,694,699,704,709,714],{"__ignoreMap":31},[72,471,472],{"class":74,"line":75},[72,473,145],{},[72,475,476],{"class":74,"line":81},[72,477,151],{"emptyLinePlaceholder":150},[72,479,480],{"class":74,"line":87},[72,481,482],{},"namespace App\\Exceptions;\n",[72,484,485],{"class":74,"line":93},[72,486,151],{"emptyLinePlaceholder":150},[72,488,489],{"class":74,"line":163},[72,490,491],{},"use Illuminate\\Foundation\\Exceptions\\Handler as ExceptionHandler;\n",[72,493,494],{"class":74,"line":169},[72,495,496],{},"use Throwable;\n",[72,498,499],{"class":74,"line":175},[72,500,501],{},"use Mail;\n",[72,503,504],{"class":74,"line":181},[72,505,506],{},"use App\\Mail\\ExceptionOccurred;\n",[72,508,509],{"class":74,"line":187},[72,510,151],{"emptyLinePlaceholder":150},[72,512,513],{"class":74,"line":192},[72,514,515],{},"class Handler extends ExceptionHandler\n",[72,517,518],{"class":74,"line":198},[72,519,84],{},[72,521,522],{"class":74,"line":203},[72,523,228],{},[72,525,526],{"class":74,"line":209},[72,527,528],{},"     * A list of the exception types that are not reported.\n",[72,530,531],{"class":74,"line":214},[72,532,240],{},[72,534,535],{"class":74,"line":220},[72,536,537],{},"     * @var array\n",[72,539,540],{"class":74,"line":225},[72,541,252],{},[72,543,544],{"class":74,"line":231},[72,545,546],{},"    protected $dontReport = [\n",[72,548,549],{"class":74,"line":237},[72,550,551],{},"        \u002F\u002F\n",[72,553,554],{"class":74,"line":243},[72,555,556],{},"    ];\n",[72,558,559],{"class":74,"line":249},[72,560,151],{"emptyLinePlaceholder":150},[72,562,563],{"class":74,"line":255},[72,564,228],{},[72,566,567],{"class":74,"line":261},[72,568,569],{},"     * A list of the inputs that are never flashed for validation exceptions.\n",[72,571,572],{"class":74,"line":267},[72,573,240],{},[72,575,576],{"class":74,"line":273},[72,577,537],{},[72,579,580],{"class":74,"line":279},[72,581,252],{},[72,583,584],{"class":74,"line":284},[72,585,586],{},"    protected $dontFlash = [\n",[72,588,589],{"class":74,"line":289},[72,590,591],{},"        'password',\n",[72,593,594],{"class":74,"line":295},[72,595,596],{},"        'password_confirmation',\n",[72,598,599],{"class":74,"line":300},[72,600,556],{},[72,602,603],{"class":74,"line":306},[72,604,151],{"emptyLinePlaceholder":150},[72,606,607],{"class":74,"line":311},[72,608,228],{},[72,610,611],{"class":74,"line":317},[72,612,613],{},"     * Register the exception handling callbacks for the application.\n",[72,615,616],{"class":74,"line":322},[72,617,240],{},[72,619,620],{"class":74,"line":328},[72,621,246],{},[72,623,624],{"class":74,"line":333},[72,625,252],{},[72,627,629],{"class":74,"line":628},36,[72,630,631],{},"    public function register()\n",[72,633,635],{"class":74,"line":634},37,[72,636,264],{},[72,638,640],{"class":74,"line":639},38,[72,641,642],{},"        $this->reportable(function (Throwable $e) {\n",[72,644,646],{"class":74,"line":645},39,[72,647,648],{},"            if ($this->shouldReport($e)) {\n",[72,650,652],{"class":74,"line":651},40,[72,653,654],{},"                $to = explode(',', env('ERROR_REPORTING_EMAIL', ''));\n",[72,656,658],{"class":74,"line":657},41,[72,659,660],{},"                Mail::to($to)->send(new ExceptionOccurred($e));\n",[72,662,664],{"class":74,"line":663},42,[72,665,666],{},"            }\n",[72,668,670],{"class":74,"line":669},43,[72,671,672],{},"        });\n",[72,674,676],{"class":74,"line":675},44,[72,677,151],{"emptyLinePlaceholder":150},[72,679,681],{"class":74,"line":680},45,[72,682,683],{},"        $this->renderable(function (Throwable $e) {\n",[72,685,687],{"class":74,"line":686},46,[72,688,648],{},[72,690,692],{"class":74,"line":691},47,[72,693,654],{},[72,695,697],{"class":74,"line":696},48,[72,698,660],{},[72,700,702],{"class":74,"line":701},49,[72,703,666],{},[72,705,707],{"class":74,"line":706},50,[72,708,672],{},[72,710,712],{"class":74,"line":711},51,[72,713,276],{},[72,715,717],{"class":74,"line":716},52,[72,718,96],{},[11,720,721],{},"There you go, now your Laravel app should be sending you email whenever an error exception is caught.",[723,724,725],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":31,"searchDepth":81,"depth":81,"links":727},[],"Laravel has a good exception reporting function built in. However, the documentation for error handling does not have instruction on sending error exceptions using email.","md",{},"\u002Fblog\u002Fexception-reporting-email-for-laravel-8","2021-06-07",{"title":6,"description":728},{"loc":731},"blog\u002Fexception-reporting-email-for-laravel-8","Software","_7huLj8SvJ_XLhRFhBVeos6XDlMJzMO0BxO9l5HfSSY",[739,743],{"title":740,"path":741,"stem":742,"children":-1},"Setup Queue Job for Laravel 8","\u002Fblog\u002Fsetup-queue-job-for-laravel-8","blog\u002Fsetup-queue-job-for-laravel-8",{"title":744,"path":745,"stem":746,"children":-1},"One Small Habit to Boost Productivity","\u002Fblog\u002Fone-small-habit-to-boost-productivity","blog\u002Fone-small-habit-to-boost-productivity",1793045552944]