Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

Laravel 5.3 send errors to email

David Carr

Laravel Framework

    Errors in Laravel are logged to files. That’s useful for monitoring when things go wrong. For more real time updates send errors by emailed. This post will explain how do set that up.

    First open app/Exceptions/Handler.php find the report method. In this method the exceptions are collected so adding an a mail send call here is ideal. the first param of send is the path to the view file. In this case resources/views/emails/errorreport.blade.php. Next pass the params in an array.

    Inside the closure set the to field and the message. 

    public function report(Exception $exception)
        Mail::send('emails.errorreport', ['e' => $exception], function($message)

    Inside resources/views/errorreport.blade.php I have:

    <h3>Error information:</h3>
    <p><strong>Date:</strong> {{ date('M d, Y H:iA') }}</p>
    <p><strong>Message:</strong> {{ $e->getMessage() }}</p>
    <p><strong>Code:</strong> {{ $e->getCode() }}</p>
    <p><strong>File:</strong> {{ $e->getFile() }}</p>
    <p><strong>Line:</strong> {{ $e->getLine() }}</p>
    <h3>Stack trace:</h3>
    <pre>{{ $e->getTraceAsString() }}</pre>

    Now every time there is an error an email will be sent.

    Laravel Modules Book by David Carr

    Help support the blog so that I can continue creating new content!

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

    © 2009 - 2022 DC Blog. All code MIT license. All rights reserved.