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

Get Email For Errors in Production – Laravel, PHP

In this article, we are going to show you, how you can receive an email 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 be display your code to the users. But for example, you get a message from one of your user saying that they are seeing an “Internal Server Error” on some page. How would you know what causes the problem to that user ?

That is where the “error reporting” comes in. There are many types of error reporting, 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 immidiately 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:

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 “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.

Leave a Reply

Please disable your adblocker or whitelist this site!