Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

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)
{
    parent::report($exception);

    Mail::send('emails.errorreport', ['e' => $exception], function($message)
    {
        $message->to('email@domain.com')->subject('error!');
    });
}

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 Your Logo Your Logo Your Logo

Become a sponsor

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

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.