I am trying to figure out how I can send errors to my email in Laravel 5. I haven\'t had much luck finding any good resources.
There used to be good packages like: http
Here you have a solution for Laravel 6.x with an html error description, trace, and all vars if somebody wants to increase, there should be a render also for the mysql queries that can be added to this html body.
Important! dont forget to put your Debug config in false, you can do that in the env file setting:
APP_DEBUG=false
Or you also can changing it in the /config/app.php file
/*
|--------------------------------------------------------------------------
| Application Debug Mode
|--------------------------------------------------------------------------
|
| When your application is in debug mode, detailed error messages with
| stack traces will be shown on every error that occurs within your
| application. If disabled, a simple generic error page is shown.
|
*/
//'debug' => env('APP_DEBUG', false),
//to
'debug' => false, //now not lookin for the envfile value
Otherway all the users are going to see a DEBUG SCREEN with a lot of private information.
The solution is to extend the Handler in /app/Exceptions/Handler.php
and replace the function Render for the next code.
public function render($request, Exception $exception)
{
$html = $this->renderExceptionWithSymfony($exception, true);
$array = $this->convertExceptionToArray($exception);
$html .= $this->renderVars($_SERVER, "Server Vars");
$html .= $this->renderVars($_REQUEST, "Requests Vars");
$html .= $this->renderVars($_COOKIE, "Cookies");
\Mail::send('emails.error', ['html'=>$html], function ($m) {
$m->to('developer@mail.com', 'Server Message')->subject('WEB ERROR');
});
return parent::render($request, $exception);
}
And also add the new function renderVars next to the new render. This functions is going to convert the Sever, request an cookies arrays to a legible
public function renderVars($vars, $titulo)
{
$html = '
'.$titulo.'
';
forEach($vars as $key=>$value)
{
$html .= '
'.$key.'
';
if(!is_array($value))
{
$html .= ''.$value.'';
}
else
{
$html .= 'ARR | '.implode(', ', $value).'';
}
$html .= '
';
$html .= ' ';
}
$html .= '
';
return $html;
}
And for the last thing you need to create a blade view in the folder /resources/views/emails/error.blade.php this blade is in this case only printing the html error formatted from render function. The mail function send array param and convert for the view.
In this case the code for the blade error template (/resources/views/emails/error.blade.php) is only:
=$html?>
That print the formated html received.
Next to that if you want to customize the error frontend screen you need to create a new blade template in the folder (/resources/views/errors/
I hope that is helpfull for other people.
best regards, Reb duvid.