philipmorrisp / laravel-exception-email-notification
当发现异常时,通过Mailable、Job Queue、EmailEventHandler发送电子邮件通知
dev-master
2017-12-06 03:16 UTC
Requires
- php: >=5.6.4
This package is not auto-updated.
Last update: 2024-09-22 14:47:11 UTC
README
- 具有发送电子邮件事件的增强型异常处理程序。
- 实现了作业队列以避免发送通知电子邮件时的额外加载时间。
- 引入了具有动态订阅方法的EmailEventHandler。
- 部分JSON响应已编写以操作异常。
- 您必须有一个运行的邮件服务器才能继续。
安装
需要Laravel 5 - 5.4。Laravel 5.5尚未测试,但您可以尝试使用它。
composer install philipmorrisp/laravel-exception-email-notification dev-master
如果您可以加载laravel包自动发现,则可以跳过添加提供者。
即使我们无法依赖于composer(例如,在生产环境中未安装composer),也仍然需要添加。
在config/app.php
的提供者数组中
# config/app.php
Philipmorrisp\LaravelExceptionEmailNotification\ExceptionServiceProvider::class,
发布文件以进行更多自定义配置。(可选)
php artisan vendor:publish --provider="Philipmorrisp\LaravelExceptionEmailNotification\ExceptionServiceProvider"
重新加载代码
composer dump-auto
php artisan config:cache
将以下代码添加到app/Providers/EventServiceProvider.php
,如果您没有此文件,则根据提供的文件创建该文件
#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();
//
}
}
在.env
的底部添加此内容。配置设置
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=
发送异常电子邮件默认使用作业队列。
# .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
高级使用
如果您想将来使用EmailEventHandler.php
来处理使用事件发送的电子邮件,则创建app/Handlers/EmailEventHandler.php
。
然后,您必须在app/Providers/EventServiceProvider.php
中将其更改为使用App\Handlers\EmailEventHandler
。
# 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);
}
}
然后,您必须重新加载代码
composer dump-auto
php artisan config:cache
要在订阅方法中使用事件,请将以下代码应用于您的类
\Illuminate\Support\Facades\Event::fire('email.eventName');
TODO
- 完成异常部分的JSON响应或与其他laravel包集成