Code Monkey home page Code Monkey logo

jeremykenedy / laravel-exception-notifier Goto Github PK

View Code? Open in Web Editor NEW
128.0 7.0 29.0 79 KB

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+. Get the errors and fix them before the client even reports them, that's why this exists! For Laravel 5, 6, and 7

License: MIT License

PHP 61.34% Blade 38.66%
email-notifications email-notification laravel-exceptions exception-handler exceptions exception-email exception-emails exception-messages

laravel-exception-notifier's Introduction

Laravel Exception Notifier | A Laravel 5, 6, 7, 8, 9 and 10 Exceptions Email Notification Package

Total Downloads Latest Stable Version Build Status StyleCI Scrutinizer Code Quality Code Intelligence Status MadeWithLaravel.com shield License: MIT

Table 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, 9, and 10.

Get the errors and fix them before the client even reports them, that's why this exists!

Requirements

Installation Instructions

  1. From your projects root folder in terminal run:

    Laravel 9-10 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
  2. 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 under providers with the following:

    jeremykenedy\laravelexceptionnotifier\LaravelExceptionNotifier::class,
  1. 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 or 10 from an older version of this package you will need to republish the assets with:

    php artisan vendor:publish --force --tag=laravelexceptionnotifier
  1. In App\Exceptions\Handler.php include the additional following classes in the head:

Laravel 9 and Above use:

    use App\Mail\ExceptionOccurred;
    use Illuminate\Support\Facades\Log;
    use Illuminate\Support\Facades\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;
  1. Update App\Exceptions\Handler.php

Laravel 9 and Above:

Add the sendEmail() method:
    /**
     * Sends an email upon exception.
     */
    public function sendEmail(Throwable $exception): void
    {
        try {
            $content = [
                'message' => $exception->getMessage(),
                'file' => $exception->getFile(),
                'line' => $exception->getLine(),
                'trace' => $exception->getTrace(),
                'url' => request()->url(),
                'body' => request()->all(),
                'ip' => request()->ip(),
            ];

            Mail::send(new ExceptionOccurred($content));
        } catch (Throwable $exception) {
            Log::error($exception);
        }
    }
Add or update the register() method:
    /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {
        $this->reportable(function (Throwable $e) {
            $enableEmailExceptions = config('exceptions.emailExceptionEnabled');

            if ($enableEmailExceptions) {
                $this->sendEmail($e);
            }
        });
    }

Laravel 8 and Below:

Replace the report() method with:
    /**
     * 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);
    }
Add the method sendEmail():
    /**
     * 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);
        }
    }
  1. Configure your email settings in the .env file.

  2. Add the following (optional) settings to your .env file and enter your settings:

    • Note: the defaults for these are located in config/exception.php
        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

Email Notification

File Tree

└── laravel-exception-notifier
    ├── .gitignore
    ├── LICENSE
    ├── composer.json
    ├── readme.md
    └── src
        ├── .env.example
        ├── App
        │   ├── Mail
        │   │   └── ExceptionOccurred.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

laravel-exception-notifier's People

Contributors

devdavido avatar jeremykenedy avatar rubicon-international avatar stylecibot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-exception-notifier's Issues

Methods in step 5 and 6 incorrect for Laravel 6

Having some trouble getting this working in Laravel 6, the methods provided in steps 5 and 6 are causing errors.

I have tried changing the 'Throwable $exception' to 'Exception $exception', and although this has now bought back the standard Laravel stack trace interface in the browser, I am not receiving emails.

As far as I can see everything else is set up correctly, and other emails from the site are sending fine using the credentials in the .env file.

Any extra steps I am missing?

Is it possible to add the logged-in User?

Thank you for this (and all your other!) open-sourced packages.

Forgive my ignorance on the specifics of the Laravel request handling stack, but would it be possible to information in the error report about which, if any, user is logged in? The base case could be a user ID with maybe a configuration option to include more columns from the User table (first, last, email, etc).

I do realize there's no guarantee this information would be available depending on when in the stack the error occurred (or if it came over an API call) but you could just display null for those cases.

What do you think? (If you think it's a good idea but don't have time, I would give it a shot if you can point me where in your package to get started and if I figure it out, I'll submit a PR.)

Thank you!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.