La mayoría de las veces no tenemos la capacidad de controlar todo lo que sucede en nuestros proyectos, me refiero a bugs. Una solución a este tipo de situaciones es controlar las excepciones que suceden para notificarnos por correo electrónico cuando algo salga mal. Para ello vamos a ver un proceso completo que te servirá en tus proyectos.
Crear un Mailable para enviar excepciones
php artisan make:mail ExceptionOccured<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ExceptionOccured extends Mailable
{
use Queueable, SerializesModels;
/**
* The body of the message.
*
* @var string
*/
public $content;
/**
* @var string
*/
public $css;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(string $content, string $css)
{
$this->content = $content;
$this->css = $css;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('mails.exception_occured');
}
}
Crea la vista mails/exception_occured.blade.php
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<style>
{!! $css !!}
</style>
</head>
<body>
{!! $content !!}
</body>
</html>Actualiza el manejador de excepciones de Laravel app/Exceptions/Handler.php
<?php
namespace App\Exceptions;
use App\Mail\ExceptionOccured;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\Mail;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (Throwable $e) {
$this->sendEmail($e);
});
}
/**
* Sends an email to the developer about the exception.
*
* @param Exception $e
* @return void
*/
public function sendEmail(Exception $e)
{
try {
$e = FlattenException::create($e);
$handler = new HtmlErrorRenderer(true);
$css = $handler->getStylesheet();
$content = $handler->getBody($e);
Mail::to('[email protected]')->send(new ExceptionOccured($content, $css));
} catch (Exception $e) {
dd($e);
}
}
}Fíjate que una de las cosas que hacemos aquí es evitar reportar errores "normales", validación, token caducado etcétera.
Con lo anterior, cada vez que algo en nuestra aplicación falle, recibiremos un correo similar al siguiente.
Curso Laravel 12
Completo 2026
El único curso 100% actualizado que incluye Laravel 12, Livewire 3, Vue 3, React 19 e Inertia 2. Aprende con proyectos reales y las últimas funcionalidades.
star Incluido en cualquier suscripción