Get Email For Errors in Production - Laravel, PHP - Tutorial

Laravel error email notification – PHP


In this article, we are going to show you, how you can receive an email notification when there is an internal server error in your Laravel live site using PHP. Internal Server Error has an error code of 500, which means there is an error in your code. When your website is live, we usually disable the “debug mode” in our “.env” file like this:


This prevents displaying the error messages to the user. Because otherwise, it will display your code to the users. But for example, you get a message from one of your users saying that they are seeing an “Internal Server Error” on some page. How would you know what causes the problem for that user ?

Log Laravel error

That is where the “error reporting” comes in. There are many types of error reporting, the most common of them is “logging” which means that you can write the error message in a separate file. But you have to check the file regularly and on daily basis.

But you must know immediately when the error happens. That is why we will be sending an email as soon as there is an error in your code.

Video Tutorial:

Handle Laravel error

First, open the file “app/Exceptions/Handler.php” and include the “Mail” and “Auth” libraries in it.

// include Mail to send an email
use Mail;

// include Auth to check if the error happened for an authenticated user
use Auth;

Then change the content of the “register” method to the following:

public function register()
    $this->reportable(function (Throwable $e) {
        // get error message
        $error_message = $e->getMessage();

        // get file
        $error_file = $e->getFile();

        // get line number
        $error_line = $e->getLine();

        // get method, GET or POST
        $method = request()->method();

        // get full URL including query string
        $full_url = request()->fullUrl();

        // get route name
        $route = "";

        // get list of all middlewares attached to that route
        $middlewares = "";

        // data with the request
        $inputs = "";
        if (request()->route() != null)
            $route = "uri: " . request()->route()->getName();
            $middlewares = json_encode(request()->route()->gatherMiddleware());
            $inputs = json_encode(request()->all());

        // get IP address of user
        $ip = request()->ip();

        // get user browser or request source
        $user_agent = request()->userAgent();

        // create email body
        $html = $error_message . "\n\n";
        $html .= "File: " . $error_file . "\n\n";
        $html .= "Line: " . $error_line . "\n\n";
        $html .= "Inputs: " . $inputs . "\n\n";
        $html .= "Method: " . $method . "\n\n";
        $html .= "Full URL: " . $full_url . "\n\n";
        $html .= "Route: " . $route . "\n\n";
        $html .= "Middlewares: " . $middlewares . "\n\n";
        $html .= "IP: " . $ip . "\n\n";
        $html .= "User Agent: " . $user_agent . "\n\n";

        // for testing purpose only

        // check if user is logged in
        if (Auth::check())
            // get email of user that faced this error
            $html .= "User: " . Auth::user()->email;

        // subject of email
        $subject = "Internal Server Error";

        // send an email
        Mail::raw($html, function ($message) use ($subject) {
            // developer email

Make sure you have set the SMTP configurations in your “.env” file:


And finally, enable the less secure apps for the above “” account from here.


You are all set. Now, if you get any error in your live site, you will receive an email notification with details. That’s how you get an email notification whenever you have an error in your Laravel application.

Learn how to enable debugbar in your Laravel application from this tutorial.

How useful was this post

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post

Leave a Reply

Please disable your adblocker or whitelist this site!