How to Send an Email on Error Exceptions in Laravel 9

https://www.itsolutionstuff.com/post/how-to-send-an-email-on-error-exceptions-in-laravel-9example.html

How to Send an Email on Error Exceptions in Laravel 9?

By Hardik Savani May 14, 2022 Category : LaravelPauseUnmuteLoaded: 1.15%Seek to live, currently behind liveLIVERemaining Time -50:07FullscreenHi Dev,

This post is focused on laravel 9 send email on exception. it's simple example of laravel 9 send exception mail. if you have question about how to send mail on exception in laravel 9 then I will give simple example with solution. This article goes in detailed on laravel 9 send email on error. Alright, let’s dive into the steps.

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 9

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

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

MAIL_MAILER=smtpMAIL_HOST=smtp.gmail.comMAIL_PORT=465MAIL_USERNAME=mygoogle@gmail.comMAIL_PASSWORD=rrnnucvnqlbslMAIL_ENCRYPTION=tlsMAIL_FROM_ADDRESS=mygoogle@gmail.comMAIL_FROM_NAME="${APP_NAME}"

Read Also: Laravel 9 Eloquent Mutators and Accessors 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.

php artisan make:mail ExceptionOccured

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 9 Create Multi Language Website Tutorial

Output: You Email Look Like This

I hope it can help you...

Last updated

Was this helpful?