Loading...

Laravel 5.3 send errors to email

David Carr

1 min read - 17th Dec, 2016

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.

0 comments
Add a comment