<?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);
}
}
<?php
namespace App\Exceptions;
use App\Mail\ExceptionOccured;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Log;
use Mail;
use Throwable;
class Handler extends ExceptionHandler {
/**
* A list of the exception types that are not reported.
*
* @var array<int, class-string<Throwable>>
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array<int, string>
*/
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);
});
}
/**
* Write code on Method
*
* @return response()
*/
public function sendEmail(Throwable $exception) {
try {
$content['message'] = $exception->getMessage();
$content['file'] = $exception->getFile();
$content['line'] = $exception->getLine();
$content['trace'] = $exception->getTrace();
$content['url'] = request()->url();
$content['body'] = request()->all();
$content['ip'] = request()->ip();
Mail::to('phamngoctuong1805@gmail.com')->send(new ExceptionOccured($content));
} catch (Throwable $exception) {
Log::error($exception);
}
}
}
C:\xampp\htdocs\reset\routes\web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/get-error', function () {
$find = App\Models\User::find(100000)->id;
return view('welcome');
});
Laravel Send an Email on Error Exceptions Tutorial
By Hardik Savani May 14, 2022 Category : LaravelPlayUnmuteLoaded: 1.01%FullscreenI 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:
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.
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
<?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; 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); }}
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.
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
<?php namespace App\Exceptions; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;use Throwable;use Log;use Mail;use App\Mail\ExceptionOccured; 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 = [ '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); }); } /** * Write code on Method * * @return response() */ public function sendEmail(Throwable $exception) { try { $content['message'] = $exception->getMessage(); $content['file'] = $exception->getFile(); $content['line'] = $exception->getLine(); $content['trace'] = $exception->getTrace(); $content['url'] = request()->url(); $content['body'] = request()->all(); $content['ip'] = request()->ip(); Mail::to('your_email@gmail.com')->send(new ExceptionOccured($content)); } catch (Throwable $exception) { Log::error($exception); } }}
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
<?php use Illuminate\Support\Facades\Route; /*|--------------------------------------------------------------------------| Web Routes|--------------------------------------------------------------------------|| Here is where you can register web routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "web" middleware group. Now create something great!|*/ Route::get('/get-error', function () { $find = App\Models\User::find(100000)->id; return view('welcome');});
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:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output: