Exception Reporting Email for Laravel 8

Software

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.

This tutorial teaches how to send an email to report when an error exception happens.

First, to save the email addresses that are going to receive the error exception reports, add this line to your .env.example file.

ERROR_REPORTING_EMAIL=

And this line to your .env file.

ERROR_REPORTING_EMAIL=youremail@example.com,yoursecondemail@example.com

Next, to create a mail job, we run this command.

php artisan make:mail ExceptionOccurred

In the app/Mail/ExceptionOccurred.php file, we need to add a public variable $content, and save the content into this variable in the constructor. The constructor now looks like this:

public function __construct($content)
{
    $this->content = $content;
}

Notice that the constructor takes in a $content argument.

In the build method, we return a blade view with the email content.

public function build()
{
    return $this->view('emails.exception')->with('content', $this->content);
}

The whole ExceptionOccurred.php file now looks like this:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ExceptionOccurred extends Mailable
{
    use Queueable, SerializesModels;

    public $content;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($content)
    {
        $this->content = $content;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.exception')->with('content', $this->content);
    }
}

Next, create a blade view file in this location: resources/views/emails/exception.blade.php

This blade file is the content that will be in the email. You can parse the exception message here.

Your Site: Error Exception
<br>
<br>
{!! $content !!}

Finally, in the app/Exceptions/Handler.php file, in the register method, add the send mail function to the handlers.

public function register()
{
    $this->reportable(function (Throwable $e) {
        if ($this->shouldReport($e)) {
            $to = explode(',', env('ERROR_REPORTING_EMAIL', ''));
            Mail::to($to)->send(new ExceptionOccurred($e));
        }
    });

    $this->renderable(function (Throwable $e) {
        if ($this->shouldReport($e)) {
            $to = explode(',', env('ERROR_REPORTING_EMAIL', ''));
            Mail::to($to)->send(new ExceptionOccurred($e));
        }
    });
}

If there are exception types that you do not wish to report, you can add them to the $dontReport array.

The whole Handler.php file now looks like this:

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Mail;
use App\Mail\ExceptionOccurred;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            if ($this->shouldReport($e)) {
                $to = explode(',', env('ERROR_REPORTING_EMAIL', ''));
                Mail::to($to)->send(new ExceptionOccurred($e));
            }
        });

        $this->renderable(function (Throwable $e) {
            if ($this->shouldReport($e)) {
                $to = explode(',', env('ERROR_REPORTING_EMAIL', ''));
                Mail::to($to)->send(new ExceptionOccurred($e));
            }
        });
    }
}

There you go, now your Laravel app should be sending you email whenever an error exception is caught.