An easy way to send emails with stack trace whenever an exception occurs on the server for Laravel applications.
composer require darshan/exceptionemail
Add exception capturing to app/Exceptions/Handler.php
:
use Throwable;
public function report(Throwable $exception)
{
app('exceptionemail')->captureException($exception);
parent::report($exception);
}
Create ExceptionEmail configuration file with this command:
php artisan vendor:publish --provider="Webmonks\ExceptionEmail\ExceptionEmailServiceProvider"
The config file will be published in config/exceptionemail.php
Following are the configuration attributes used for the ExceptionEmail.
The package comes with 'silent' => true,
configuration by default, since you probably don't want error emailing enabled on your development environment. Especially if you've set 'debug' => true,
.
'silent' => env('IS_EXCEPTION_EMAIL_SILENT', true),
For sending emails when an exception occurs set IS_EXCEPTION_EMAIL_SILENT=false
in your .env
file.
It contains the list of the exception types that should be captured. You can add your exceptions here for which you want to send error emails.
By default, the package has included Symfony\Component\Debug\Exception\FatalErrorException::class
.
'capture' => [
Symfony\Component\Debug\Exception\FatalErrorException::class,
],
You can also use '*'
in the $capture
array which will, in return captures every exception.
'capture' => [
'*'
],
By default, the package has included nothing to ignore exception email list
'ignored_exception' => [
// Webmonks\ExceptionEmail\Exceptions\DummyException::class,
],
But if you want to ignore any specific type of exceptions you can add in the $ignored_exception
array which will, in return ignored those specified exceptions.
'ignored_exception' => [
Symfony\Component\Debug\Exception\FatalErrorException::class,
],
To use this feature you need to add the following code in app/Exceptions/Handler.php
:
public function report(Exception $exception)
{
if ($this->shouldReport($exception)) {
app('exceptionemail')->captureException($exception);
}
parent::report($exception);
}
It's a list of recipients who will receive error emails.
'to' => [
// 'hello@example.com',
],
It's a list of bots for where you would like to ignore sending error emails.
'ignored_bots' => [
'googlebot', // Googlebot
'bingbot', // Microsoft Bingbot
'slurp', // Yahoo! Slurp
'ia_archiver', // Alexa
],
Run the following command in order to customize the subject and body of the email
php artisan vendor:publish --provider="Webmonks\ExceptionEmail\ExceptionEmailServiceProvider"
Note - Don't run this command again if you have already run it.
Now, subject and body views are located in the resources/views/vendor/exceptionemail
directory for the emails.
We have passed the thrown exception object $exception
in a view which you can use to customize according to your need.
To verify ExceptionEmail is configured correctly and our integration is working, use exceptionemail:test
Artisan command:
php artisan exceptionemail:test
A Webmonks\ExceptionEmail\Exceptions\DummyException
class will be thrown and captured by ExceptionEmail. The captured exception will appear in your configured email immediately.
If you discover any security-related issues, please email damku999@gmail.com instead of using the issue tracker.
Webmonks is a startup company for product development based in Ahmedabad, India. You'll find an overview of all our open source projects on GitHub.
The MIT License. Please see License File for more information. Copyright © 2020 Webmonks