If you have deployed your Laravel web application on a live server and want to receive email notifications whenever an error occurs, you can utilize Laravel’s App\Exceptions\Handler
class. This tutorial will guide you through the process of setting up error exception email notifications in your Laravel application.
In this tutorial, you will learn how to set up error exception mail notifications in your Laravel application. This will allow you to receive email notifications whenever an error occurs in your application, ensuring that you are promptly alerted to any issues.
Laravel 10 Send an Email on Error Exceptions Tutorial
By using the following steps, you can set up error exception mail notifications in your Laravel application:
- Step 1: Create Mail Class
- Step 2: Create an Email View
- Step 3: Configure Handler.php File for Exception Mail
Step 1: Create Mail Class
First of all, create a mail class using the below command:
php artisan make:mail ExceptionMail
This will create a class ExceptionMail in the app/Mail
directory.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class ExceptionMail extends Mailable
{
use Queueable, SerializesModels;
/**
* The body of the message.
*
* @var string
*/
public $content;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($content)
{
$this->content = $content;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.exception_email')
->with('content', $this->content);
}
}
Step 2: Create an Email View
Now, we will create a new view file inside the emails folder that file name email_exception.blade.php. and the below-given code in add your email_exception.blade.php file:
{!! $content !!}
Step 3: Configure Handler.php File for Exception Mail
Go to App\Exceptions\Handler file and update the below code into your file.
<?php
namespace App\Exceptions;
use Mail;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
use App\Mail\ExceptionOccured;
use Throwable;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Exception $exception
* @return void
*/
public function report(Throwable $exception)
{
if ($this->shouldReport($exception)) {
$this->sendEmail($exception); // sends an email
}
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
return parent::render($request, $exception);
}
public function sendEmail(Throwable $exception)
{
try {
$e = FlattenException::create($exception);
$handler = new SymfonyExceptionHandler();
$html = $handler->getHtml($e);
Mail::to('[email protected]')->send(new ExceptionMail($html));
} catch (Exception $ex) {
dd($ex);
}
}
}
Replace '[email protected]'
with the email address where you want to receive error notifications.
Now, whenever an error occurs in your Laravel application, Laravel will automatically trigger the report
method in the Handler
class. This method will send an email containing the error details to the specified email address.
Make sure you have configured a mail driver in your Laravel application’s .env
file or the config/mail.php
file to enable email sending.
Conclusion
With this setup, you will receive an email notification whenever an error occurs in your Laravel web application on the live server. This can help you promptly identify and address any issues that may arise.
Recommended Laravel Posts