Laravel 5 Send Errors to Email

前端 未结 9 649
野的像风
野的像风 2021-02-03 10:45

I am trying to figure out how I can send errors to my email in Laravel 5. I haven\'t had much luck finding any good resources.

There used to be good packages like: http

9条回答
  •  太阳男子
    2021-02-03 10:58

    You can do this by catching all the errors in the App\Exceptions\Handler::report(). So in you App/Exceptions/Handler.php add a report function if its not already there.

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(\Exception $e)
    {
        if ($e instanceof \Exception) {
            // emails.exception is the template of your email
            // it will have access to the $error that we are passing below
            Mail::send('emails.exception', ['error' => $e->getMessage()], function ($m) {
                $m->to('your email', 'your name')->subject('your email subject');
            });
        }
    
        return parent::report($e);
    }
    

    If you need more info, refer to laravel documentation form mailer and errors.

提交回复
热议问题