Package
Laravel Exception Notifier | A Laravel 5, 6, 7, 8, and 9 Exceptions Email NotificationTable of contents:
About
Laravel exception notifier will send an email of the error along with the stack trace to the chosen recipients. This Package includes all necessary traits, views, configs, and Mailers for email notifications upon your applications exceptions. You can customize who send to, cc to, bcc to, enable/disable, and custom subject or default subject based on environment. Built for Laravel 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 6, 7, 8, and 9+.
Get the errors and fix them before the client even reports them, that's why this exists!
Requirements
Installation Instructions
-
From your projects root folder in terminal run:
Laravel 9+ use:
composer require jeremykenedy/laravel-exception-notifier
Laravel 7-8 use:
composer require jeremykenedy/laravel-exception-notifier:2.2.0
Laravel 6 and below use:
composer require jeremykenedy/laravel-exception-notifier:1.2.0
-
Register the package
-
Laravel 5.5 and up Uses package auto discovery feature, no need to edit the
config/app.php
file. -
Laravel 5.4 and below Register the package with laravel in
config/app.php
underproviders
with the following:
jeremykenedy\laravelexceptionnotifier\LaravelExceptionNotifier::class,
- Publish the packages view, mailer, and config files by running the following from your projects root folder:
php artisan vendor:publish --tag=laravelexceptionnotifier
NOTE: If upgrading to Laravel 9 from an older version of this package you will need to republish the assets with:
php artisan vendor:publish --force --tag=laravelexceptionnotifier
- In
App\Exceptions\Handler.php
include the additional following classes in the head:
Laravel 9 and Above use:
use App\Mail\ExceptionOccured;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\Log;
use Mail;
use Throwable;
Laravel 8 and Below use:
use App\Mail\ExceptionOccured;
use Illuminate\Support\Facades\Log;
use Mail;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
- Update
App\Exceptions\Handler.php
Laravel 9 and Above:
App\Exceptions\Handler.php
replace the register()
method with:
In /**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (Throwable $e) {
$this->sendEmail($e);
});
}
Laravel 8 and Below:
App\Exceptions\Handler.php
replace the report()
method with:
In /**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Throwable $exception
*
* @return void
*/
public function report(Throwable $exception)
{
$enableEmailExceptions = config('exceptions.emailExceptionEnabled');
if ($enableEmailExceptions === '') {
$enableEmailExceptions = config('exceptions.emailExceptionEnabledDefault');
}
if ($enableEmailExceptions && $this->shouldReport($exception)) {
$this->sendEmail($exception);
}
parent::report($exception);
}
- In
App\Exceptions\Handler.php
add the methodsendEmail()
:
Laravel 9 and Above:
/**
* Sends an email upon exception.
*
* @param \Throwable $exception
*
* @return void
*/
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::send(new ExceptionOccured($content));
} catch (Throwable $exception) {
Log::error($exception);
}
}
Laravel 8 and Below:
/**
* Sends an email upon exception.
*
* @param \Throwable $exception
*
* @return void
*/
public function sendEmail(Throwable $exception)
{
try {
$e = FlattenException::create($exception);
$handler = new SymfonyExceptionHandler();
$html = $handler->getHtml($e);
Mail::send(new ExceptionOccured($html));
} catch (Throwable $exception) {
Log::error($exception);
}
}
-
Configure your email settings in the
.env
file. -
Add the following (optional) settings to your
.env
file and enter your settings:- Note: the defaults for these are located in
config/exception.php
- Note: the defaults for these are located in
EMAIL_EXCEPTION_ENABLED=false
EMAIL_EXCEPTION_FROM="${MAIL_FROM_ADDRESS}"
EMAIL_EXCEPTION_TO='[email protected], [email protected]'
EMAIL_EXCEPTION_CC=''
EMAIL_EXCEPTION_BCC=''
EMAIL_EXCEPTION_SUBJECT=''
Screenshots
File Tree
└── laravel-exception-notifier
├── .gitignore
├── LICENSE
├── composer.json
├── readme.md
└── src
├── .env.example
├── App
│  ├── Mail
│  │  └── ExceptionOccured.php
│  └── Traits
│  └── ExceptionNotificationHandlerTrait.php
├── LaravelExceptionNotifier.php
├── config
│  └── exceptions.php
└── resources
└── views
└── emails
└── exception.blade.php
- Tree command can be installed using brew:
brew install tree
- File tree generated using command
tree -a -I '.git|node_modules|vendor|storage|tests'
License
Laravel-Exception-Notifier | A Laravel Exceptions Email Notification Package is open-sourced software licensed under the MIT license