2017 © Pedro Peláez
 

package laravel-exception-email-notification

Send an email notification when found an exception with Mailable, Job Queue, EmailEventHandler

image

philipmorrisp/laravel-exception-email-notification

Send an email notification when found an exception with Mailable, Job Queue, EmailEventHandler

  • Wednesday, December 6, 2017
  • by philipmorrisp
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Laravel-Exception-Email-Notification

  • An enhanced exception handler with send email Event.
  • Job Queue is implemented to avoid the additional loading time to send notification email.
  • An EmailEventHandler is introduced with dynamic subscribe method.
  • Part of JSON Response has been written for manipulating Exception
  • You must have a running mail server to go further

Install

Requires Laravel 5 - 5.4. Laravel 5.5 is not tested but you could give it a try., (*1)

composer install philipmorrisp/laravel-exception-email-notification dev-master

You can skip adding provider if you can load the laravel package auto-discovery., (*2)

Still it will be needed to add if we cannot depend on composer (e.g. On production without composer installed), (*3)

in the providers array of config/app.php:, (*4)

# config/app.php

Philipmorrisp\LaravelExceptionEmailNotification\ExceptionServiceProvider::class,

Publish files to app for more custom configuration. (optional), (*5)

php artisan vendor:publish --provider="Philipmorrisp\LaravelExceptionEmailNotification\ExceptionServiceProvider"

Reload code, (*6)

composer dump-auto
php artisan config:cache

Add this code to app/Providers/EventServiceProvider.php, if you do not have this file, create the file as provided:, (*7)

#app/Providers/EventServiceProvider.php

protected $subscribe = [
    ...
    'App\Handlers\EmailEventHandler',
];

--------

# The following will be required ONLY if you do not have app/Providers/EventServiceProvider.php
# Add the following in the providers array of config/app.php:

App\Providers\EventServiceProvider::class,

# Create app/Providers/EventServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\SomeEvent' => [
            'App\Listeners\EventListener',
        ],
    ];

    protected $subscribe = [
        'Philipmorrisp\LaravelExceptionEmailNotification\EmailEventHandler',
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        //
    }
}

Add this in the bottom of .env. Configure the settings, (*8)

EMAIL_EXCEPTION_ENABLED=false
EMAIL_EXCEPTION_FROM=name@domain.com
EMAIL_EXCEPTION_FROM_NAME='ABC'
EMAIL_EXCEPTION_TO='name@domain.com,name@domain.com'
EMAIL_EXCEPTION_CC=
EMAIL_EXCEPTION_BCC=
EMAIL_EXCEPTION_SUBJECT=

The sending exception email used default Job Queue., (*9)

# .env
QUEUE_DRIVER=database

# Configure settings in config/queue.php > connection > database
    ...
    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'retry_after' => 90,
    ],
    ...

# Run the following command

php artisan config:cache
php artisan queue:table
php artisan migrate

# Listen to the queue
php artisan queue:work database --daemon --tries=3

Advanced Use

If you want to use EmailEventHandler.php for handling send email using Event in the future, create app/Handlers/EmailEventHandler.php., (*10)

Then you must change to use App\Handlers\EmailEventHandler in the app/Providers/EventServiceProvider.php instead., (*11)

# app/Handlers/EmailEventHandler.php
<?php

namespace App\Handlers;


use Config;
use Exception;
use Philipmorrisp\LaravelExceptionEmailNotification\EmailEventHandler as BaseEmailEventHandler;

class EmailEventHandler extends BaseEmailEventHandler
{
    /**
     * @param $events
     */
    public function subscribe($events)
    {
        //Add your custom subscribe events here. 
        //Then the 'eventName' will load app/Mail/MethodName.php 
        //(Capitalized methodName as the mailable php file name and class name) , Mailable Class will be used.
        //You can take a look at app/Mail/ExceptionOccured.php to see how to create a Mailable Queueable class

        //Example
        //$events->listen('email.eventName', self::class . '@methodName');

        parent::subscribe($events);
    }

    /**
     *
     * Dynamically called methods
     *
     * @param $method
     * @param $parameters
     */
    public function __call($method, $parameters)
    {
        // Custom code can be written here...

        parent::__call($method, $parameters);
    }
}

Then you must reload code, (*12)

composer dump-auto
php artisan config:cache

To use the event in the subscribe method, apply the following code in your classes:, (*13)

\Illuminate\Support\Facades\Event::fire('email.eventName');, (*14)

TODO:

  • Finish the part of JSON Response on Exceptions or integrate with other laravel packages

The Versions

06/12 2017

dev-master

9999999-dev

Send an email notification when found an exception with Mailable, Job Queue, EmailEventHandler

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

by philipmorris1697

laravel email exception event notification job queue