Laravel Send an Email on Error Exceptions Tutorial (ok)
https://www.itsolutionstuff.com/post/laravel-send-an-email-on-error-exceptions-tutorialexample.html
C:\xampp\htdocs\reset\app\Mail\ExceptionOccured.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ExceptionOccured extends Mailable {
use Queueable, SerializesModels;
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')->with('content', $this->content);
}
}C:\xampp\htdocs\reset\resources\views\emails\exception.blade.php
C:\xampp\htdocs\reset\app\Exceptions\Handler.php
C:\xampp\htdocs\reset\routes\web.php

Laravel Send an Email on Error Exceptions Tutorial
By Hardik Savani May 14, 2022 Category : LaravelPlayUnmuteLoaded: 1.01%Fullscreen
I am going to show you example of laravel send email on exception. step by step explain laravel send exception mail. if you have question about how to send mail on exception in laravel then I will give simple example with solution. you can see laravel send email on error.
You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 version.
Sometimes we upload code on production and when you have any error on production then we don't know it. Even laravel log on laravel.log file but as a developer, we don't check every day on the log file. so you need something that will inform you when error or exception generate on your application. I will give you the perfect solution for this. When any error or exception generate in your laravel project then it will inform you via email. Yes, We will send an email on Error Exceptions in laravel.
It's totally free. So just follow the below step and add send mail on an error in laravel app.
Step 1: Install Laravel
This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:
Step 2: Make Configuration
In first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel 9 will use those sender configuration for sending email. So you can simply add as like following.
.env

Read Also: Laravel Import Large SQL File using Seeder Example
Step 3: Create Mail Class
In this step we will create mail class ExceptionOccured for email send on error exception. So let's run bellow command.
now, let's update code on ExceptionOccured.php file as bellow:
app/Mail/ExceptionOccured.php
Step 4: Create Blade View
In this step, we will create blade view file for email. In this file we will write all exception details. now we just write some dummy text. create bellow files on "emails" folder.
resources/views/emails/exception.blade.php
Step 5: Send Mail on Exception
Here, every error exception handle on Exceptions/Handler.php file in laravel. in this file we will write code for send email with error message, file, line, trace, url and ip address details. you can get more details and send in email.
You can also store this information on database from here. so let's copy below code and paste on Handler.php file. You need to change recipient email address.
app/Exceptions/Handler.php
Step 6: Create Routes
In this step, we will add one route that generate exception and you will receive one email notification. We are creating this is a testing route. so let's add it.
routes/web.php
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
Now, Go to your web browser, type the given URL and view the app output:
Read Also: Laravel Webcam Capture Image and Save from Camera Example
Output: You Email Look Like This

I hope it can help you...
Last updated
Was this helpful?