Laravel 5 Send Errors to Email
EDIT: I found a 3rd party log system built for Laravel
www.understand.io
Very nice solution, doesn't email me, but this works for what I need.
I played around with this and went through the Laravel core files and came up with something similar to what you see on an error page.
You just need to create a view file that echos out $content for the email
public function report(\Exception $e)
{
if ($e instanceof \Exception) {
$debugSetting = Config::get('app.debug');
Config::set('app.debug', true);
if (ExceptionHandler::isHttpException($e)) {
$content = ExceptionHandler::toIlluminateResponse(ExceptionHandler::renderHttpException($e), $e);
} else {
$content = ExceptionHandler::toIlluminateResponse(ExceptionHandler::convertExceptionToResponse($e), $e);
}
Config::set('app.debug', $debugSetting);
$data['content'] = (!isset($content->original)) ? $e->getMessage() : $content->original;
Mail::queue('errors.emails.error', $data, function ($m) {
$m->to('[email protected]', 'Server Message')->subject('Error');
});
}
return parent::report($e);
}
You can do this by catching all the errors in the App\Exceptions\Handler::report()
. So in you App/Exceptions/Handler.php
add a report
function if its not already there.
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(\Exception $e)
{
if ($e instanceof \Exception) {
// emails.exception is the template of your email
// it will have access to the $error that we are passing below
Mail::send('emails.exception', ['error' => $e->getMessage()], function ($m) {
$m->to('your email', 'your name')->subject('your email subject');
});
}
return parent::report($e);
}
If you need more info, refer to laravel documentation form mailer and errors.