Skip to content

Commit cc31f72

Browse files
initial package
0 parents  commit cc31f72

File tree

10 files changed

+458
-0
lines changed

10 files changed

+458
-0
lines changed

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
### OSX BOOGERS ###
2+
.DS_Store
3+
*._DS_Store
4+
._.DS_Store
5+
*._
6+
._*
7+
._.*
8+
9+
### WINDOWS BOOGERS ###
10+
Thumbs.db
11+
12+
### Sass ###
13+
/.sass-cache/*
14+
.sass-cache
15+
16+
### SUBLIMETEXT BOOGERS ###
17+
*.sublime-workspace
18+
19+
### PHPSTORM BOOGERS ###
20+
.idea/*
21+
/.idea/*
22+
23+
### DIFFERENT TYPE OF MASTER CONFIGS ###
24+
composer.lock
25+
.env
26+
27+
### ASSET EXCLUSIONS ###
28+
vendor/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 <Jeremy Kenedy>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "jeremykenedy/laravel-exception-notifier",
3+
"description": "Laravel exception notifier will send an email of of the error along with the stack trace to the chosen recipients.",
4+
"keywords": ["laravel", "exception", "notification", "email"],
5+
"license": "MIT",
6+
"type": "package",
7+
"authors": [
8+
{
9+
"name": "jeremykenedy",
10+
"email": "jeremykenedy@gmail.com"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.6.4",
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"jeremykenedy\\laravelexceptionnotifier\\": "src/"
19+
}
20+
},
21+
"minimum-stability": "dev",
22+
"prefer-stable": true
23+
}

readme.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Laravel-Exception-Notifier | A Laravel Excpetions Email Notification [Package](https://packagist.org/packages/jeremykenedy/laravel-exception-notifier) | v0.0.1
2+
3+
[![Total Downloads](https://poser.pugx.org/jeremykenedy/laravel-exception-notifier/d/total.svg)](https://packagist.org/packages/jeremykenedy/laravel-exception-notifier)
4+
[![Latest Stable Version](https://poser.pugx.org/jeremykenedy/laravel-exception-notifier/v/stable.svg)](https://packagist.org/packages/jeremykenedy/laravel-exception-notifier)
5+
[![License](https://poser.pugx.org/jeremykenedy/laravel-exception-notifier/license.svg)](https://packagist.org/packages/jeremykenedy/laravel-exception-notifier)
6+
7+
## Introduction
8+
9+
Laravel exception notifier will send an email of of the error along with the stack trace to the chosen recipients. [This Package](https://packagist.org/packages/jeremykenedy/laravel-exception-notifier) 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, and 5.4+.
10+
11+
## Requirements
12+
13+
* [Laravel 5.2, 5.3, or 5.4 or newer](https://laravel.com/docs/installation)
14+
15+
## Installation
16+
17+
1. From your projects root folder in terminal run:
18+
19+
```
20+
composer require jeremykenedy/laravel-exception-notifier
21+
```
22+
23+
2. Register the package with laravel in `config/app.php` under `providers` with the following:
24+
25+
```
26+
jeremykenedy\laravelexceptionnotifier\LaravelExceptionNotifier::class,
27+
```
28+
29+
3. Publish the packages language files by running the following from your projects root folder:
30+
31+
```
32+
php artisan vendor:publish --tag=laravelexceptionnotifier
33+
```
34+
35+
5. Configure your email settings in the `.env` file.
36+
37+
5. Add the following (optional) settings to your `.env` file and enter your settings:
38+
39+
* Note: the defaults for these are located in `config/exception.php`
40+
41+
```
42+
EMAIL_EXCEPTION_ENABLED=false
43+
EMAIL_EXCEPTION_FROM=email@email.com
44+
EMAIL_EXCEPTION_TO='email1@gmail.com, email2@gmail.com'
45+
EMAIL_EXCEPTION_CC=''
46+
EMAIL_EXCEPTION_BCC=''
47+
EMAIL_EXCEPTION_SUBJECT=''
48+
```
49+
50+
## Screenshots
51+
52+
![Email Notification]( {IMAGE_URL} )
53+
54+
## License
55+
56+
Laravel-Exception-Notifier | A Laravel Excpetions Email Notification Package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

src/.env.example

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
APP_NAME=Laravel
2+
APP_ENV=local
3+
APP_KEY=
4+
APP_DEBUG=true
5+
APP_LOG_LEVEL=debug
6+
APP_URL=http://localhost
7+
8+
DB_CONNECTION=mysql
9+
DB_HOST=127.0.0.1
10+
DB_PORT=3306
11+
DB_DATABASE=homestead
12+
DB_USERNAME=homestead
13+
DB_PASSWORD=secret
14+
15+
BROADCAST_DRIVER=log
16+
CACHE_DRIVER=file
17+
SESSION_DRIVER=file
18+
QUEUE_DRIVER=sync
19+
20+
REDIS_HOST=127.0.0.1
21+
REDIS_PASSWORD=null
22+
REDIS_PORT=6379
23+
24+
MAIL_DRIVER=smtp
25+
MAIL_HOST=smtp.mailtrap.io
26+
MAIL_PORT=2525
27+
MAIL_USERNAME=null
28+
MAIL_PASSWORD=null
29+
MAIL_ENCRYPTION=null
30+
31+
PUSHER_APP_ID=
32+
PUSHER_APP_KEY=
33+
PUSHER_APP_SECRET=
34+
35+
# Email Expection Settings are optional as defaults are set in config/exceptions.php - below are example values
36+
EMAIL_EXCEPTION_ENABLED=false
37+
EMAIL_EXCEPTION_FROM=jeremykenedy@gmail.com
38+
EMAIL_EXCEPTION_TO='jeremyekenedy@gmail.com,jeremykenedy@gmail.com'
39+
EMAIL_EXCEPTION_CC=''
40+
EMAIL_EXCEPTION_BCC=''
41+
EMAIL_EXCEPTION_SUBJECT=''

src/App/Mail/ExceptionOccured.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace App\Mail;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Mail\Mailable;
7+
use Illuminate\Queue\SerializesModels;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
10+
class ExceptionOccured extends Mailable
11+
{
12+
use Queueable, SerializesModels;
13+
14+
private $content;
15+
16+
/**
17+
* Create a new message instance.
18+
*
19+
* @return void
20+
*/
21+
public function __construct($content)
22+
{
23+
$this->content = $content;
24+
}
25+
26+
/**
27+
* Build the message.
28+
*
29+
* @return $this
30+
*/
31+
public function build()
32+
{
33+
34+
$emailsTo = str_getcsv(config('exceptions.emailExceptionsTo'),',');
35+
$ccEmails = str_getcsv(config('exceptions.emailExceptionCCto'),',');
36+
$bccEmails = str_getcsv(config('exceptions.emailExceptionBCCto'),',');
37+
$fromSender = config('exceptions.emailExceptionFrom');
38+
$subject = config('exceptions.emailExceptionSubject');
39+
40+
if ($emailsTo[0] == null) {
41+
$emailsTo = config('exceptions.emailExceptionsToDefault');
42+
}
43+
44+
if ($ccEmails[0] == null) {
45+
$ccEmails = config('exceptions.emailExceptionCCtoDefault');
46+
}
47+
48+
if ($bccEmails[0] == null) {
49+
$bccEmails = config('exceptions.emailExceptionBCCtoDefault');
50+
}
51+
52+
if (!$fromSender) {
53+
$fromSender = config('exceptions.emailExceptionFromDefault');
54+
}
55+
56+
if (!$subject) {
57+
$subject = config('exceptions.emailExceptionSubjectDefault');
58+
}
59+
60+
return $this->from($fromSender)
61+
->to($emailsTo)
62+
->cc($ccEmails)
63+
->bcc($bccEmails)
64+
->subject($subject)
65+
->view(config('exceptions.emailExceptionView'))
66+
->with('content', $this->content);
67+
}
68+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
use App\Mail\ExceptionOccured;
6+
use Mail;
7+
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
8+
use Symfony\Component\Debug\Exception\FlattenException;
9+
10+
trait ExceptionNotificationHandlerTrait
11+
{
12+
13+
/**
14+
* A list of the exception types that should not be reported.
15+
*
16+
* @var array
17+
*/
18+
protected $dontReport = [
19+
\Illuminate\Auth\AuthenticationException::class,
20+
\Illuminate\Auth\Access\AuthorizationException::class,
21+
\Symfony\Component\HttpKernel\Exception\HttpException::class,
22+
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
23+
\Illuminate\Session\TokenMismatchException::class,
24+
\Illuminate\Validation\ValidationException::class,
25+
];
26+
27+
/**
28+
* Report or log an exception.
29+
*
30+
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
31+
*
32+
* @param \Exception $exception
33+
* @return void
34+
*/
35+
public function report(Exception $exception)
36+
{
37+
38+
$enableEmailExceptions = config('exceptions.emailExceptionEnabled');
39+
40+
if ($enableEmailExceptions === "") {
41+
$enableEmailExceptions = config('exceptions.emailExceptionEnabledDefault');
42+
}
43+
44+
if ($enableEmailExceptions) {
45+
if ($this->shouldReport($exception)) {
46+
$this->sendEmail($exception);
47+
}
48+
}
49+
50+
parent::report($exception);
51+
}
52+
53+
/**
54+
* Sends an email upon exception.
55+
*
56+
* @param \Exception $exception
57+
* @return void
58+
*/
59+
public function sendEmail(Exception $exception)
60+
{
61+
try {
62+
63+
$e = FlattenException::create($exception);
64+
$handler = new SymfonyExceptionHandler();
65+
$html = $handler->getHtml($e);
66+
67+
Mail::send(new ExceptionOccured($html));
68+
69+
} catch (Exception $exception) {
70+
71+
dd($exception);
72+
73+
}
74+
}
75+
76+
77+
}

src/LaravelExceptionNotifier.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace jeremykenedy\laravelexceptionnotifier;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class LaravelExceptionNotifier extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap the application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
17+
// Publish Trait
18+
$this->publishes([
19+
__DIR__.'/App/Traits/ExceptionNotificationHandlerTrait.php' => app_path('Traits/ExceptionNotificationHandlerTrait.php'),
20+
], 'laravelexceptionnotifier');
21+
22+
// Publish Mailer
23+
$this->publishes([
24+
__DIR__.'/App/Mail/ExceptionOccured.php' => app_path('Mail/ExceptionOccured.php'),
25+
], 'laravelexceptionnotifier');
26+
27+
// Publish email view
28+
$this->publishes([
29+
__DIR__.'/resources/views/emails/exception.blade.php' => resource_path('views/emails/exception.blade.php'),
30+
], 'laravelexceptionnotifier');
31+
32+
// Publish config file
33+
$this->publishes([
34+
__DIR__.'/config/exceptions.php' => config_path('exceptions.php'),
35+
], 'laravelexceptionnotifier');
36+
37+
}
38+
39+
/**
40+
* Register the application services.
41+
*
42+
* @return void
43+
*/
44+
public function register()
45+
{
46+
// Update App with Trait
47+
$this->app->bind(
48+
'App\Traits\ExceptionNotificationHandlerTrait'
49+
);
50+
}
51+
}

0 commit comments

Comments
 (0)