yhw/laravel-email-database-log

为Laravel网站发送的所有邮件提供简单数据库日志工具 - 由shvetsgroup/laravel-email-database-log构建。

v1.0.0 2023-11-19 23:50 UTC

This package is auto-updated.

Last update: 2024-09-30 01:52:26 UTC


README

为Laravel网站发送的所有邮件提供简单数据库日志工具。由ShvetsGroup\LaravelEmailDatabaseLog分支而来。

对于Laravel 5.8 - 使用2.*版本

对于Laravel 6 - 使用3.*版本

对于Laravel 7 - 使用4.*版本

对于Laravel 8 - 使用5.*版本

版本5.1使用文件系统,这是从5.3开始的重大变更。有关升级信息,请参阅下面。

自定义模板

要使邮件日志与您的Laravel主题更好地集成,请使用EMAIL_LOG_CUSTOM_TEMPLATE选项在.env文件中设置您的blade文件路径。

我使用的blade文件位于custom_templates文件夹中。

在我的项目中,它们位于resources\views\admin\templates\email_logger\

我的.env文件中包含以下行

EMAIL_LOG_ACCESS_MIDDLEWARE="admin"
EMAIL_LOG_ROUTES_PREFIX="admin"
EMAIL_LOG_CUSTOM_TEMPLATE="admin.templates.email_logger.email-logger"

安装

步骤1:Composer

您可以通过在终端运行以下命令来通过composer安装Laravel Email Database Log:

composer require yhw/laravel-email-database-log

步骤2:配置

如果您使用的Laravel版本是5.5或更高版本,则可以跳过此步骤。否则,您必须将以下内容添加到config/app.php中的providers数组:

'providers' => [
    // ...
    Yhw\LaravelEmailDatabaseLog\LaravelEmailDatabaseLogServiceProvider::class,
],

步骤3:迁移

现在,请在终端运行以下命令:

php artisan migrate

步骤4:配置

要发布配置文件,请在终端运行以下命令:

php artisan vendor:publish --provider="Yhw\LaravelEmailDatabaseLog\LaravelEmailDatabaseLogServiceProvider"

配置包含以下参数

//name of the disk where the attachments will be saved
'disk' => env('EMAIL_LOG_DISK','email_log_attachments'),

//template override
'custom_template' => env('EMAIL_LOG_CUSTOM_TEMPLATE',null),

//to prevent access to list of logged emails through WEB routes add a middlewares
//multiple middlewares can be used (separate by comma)
'access_middleware' => env('EMAIL_LOG_ACCESS_MIDDLEWARE',null),

//to prevent access to list of logged emails through API routes add a middlewares
//multiple middlewares can be used (separate by comma)
'access_middleware_api' => env('EMAIL_LOG_ACCESS_MIDDLEWARE_API',null),

//this parameter prefixes the routes for listing of logged emails using WEB routes
'routes_prefix' => env('EMAIL_LOG_ROUTES_PREFIX',''),

//this parameter prefixes the routes for listing of logged emails using API routes
'routes_prefix_api' => env('EMAIL_LOG_ROUTES_PREFIX_API',''),

//custom route prefix for webhooks
'routes_webhook_prefix' => env('EMAIL_LOG_ROUTES_WEBHOOK_PREFIX', env('EMAIL_LOG_ROUTES_PREFIX','')),

//mailgun secret and whether to ommit events for emails which were not found
'mailgun' => [
    'secret' => env('MAILGUN_SECRET', null),
    'filter_unknown_emails' => env('EMAIL_LOG_MAILGUN_FILTER_UNKNOWN_EMAILS', true),
],

使用方法

安装后,您网站发送的任何邮件都将记录到网站数据库的email_log表中。

任何附件都将保存在storage/email_log_attachments磁盘上。可以通过发布配置文件并更改'folder'值来更改email_log_attachments。

您还需要在config/filesystems.php文件中添加以下磁盘

'email_log_attachments' => [
    'driver' => 'local',
    'root' => storage_path('app/email_log_attachments'),
],

如果您想处理记录的邮件或在您的系统上保存/格式化一些附加数据,您可以通过Laravel监听器来连接到Yhw\LaravelEmailDatabaseLog\LaravelEvents\EmailLogged事件

https://laravel.net.cn/docs/5.5/events#defining-listeners

不要忘记注册事件

https://laravel.net.cn/docs/5.5/events#registering-events-and-listeners

如果您使用的是Laravel >=5.8.9,则可以使用事件发现

https://laravel.net.cn/docs/5.8/events#registering-events-and-listeners

如果您在服务器上使用队列,则需要重新启动工作进程以使库工作

Remember, queue workers are long-lived processes and store the booted application state in memory.
As a result, they will not notice changes in your code base after they have been started.
So, during your deployment process, be sure to restart your queue workers.


https://laravel.net.cn/docs/5.5/queues#running-the-queue-worker

您可以使用以下URI /email-log来审查已发送的邮件。

您可以在.env文件中添加类似EMAIL_LOG_ROUTES_PREFIX=prefix/的内容来前缀此URI。

您可以通过添加类似EMAIL_LOG_ACCESS_MIDDLEWARE=auth,landlord的内容到.env文件来使用中间件保护此URI。

MailGun webhooks

您可以使用Mailgun webhooks来记录webhook事件。在MailGun Webhooks部分添加

https://example.com/email-log/webhooks/event

对所有事件。如果您在配置文件中使用了prefix,则这应该在URL中反映出来

https://example.com/your-prefix/email-log/webhooks/event

从5.2.2升级到5.3.0 - API ENDPOINTS 可用,重大变更

API ENDPOINTS 可用

带有JSON响应的新路由

https://example.com/api/your-prefix/email-log
https://example.com/api/your-prefix/email-log/{id}
https://example.com/api/your-prefix/email-log/{id}/attachment/{attachment}
https://example.com/api/your-prefix/email-log/delete

重大变更

显示附件的逻辑(在单个邮件视图show.blade.php中)已移动到后端。如果您不是使用默认的show.blade.php并且正在显示附件列表,则应在您的代码中反映这些更改。具体来说,您不需要检查文件是否存在或向路由添加附件以访问它。相反,返回的数据将是以下格式的数组(根据文件是否可在磁盘上找到而定)

//file can be found
[
    'name' => 'filename.pdf',
    'route' => 'https://example.com/api/email-log/email-id/attachment/attachment-key',
]
//file can't be found
[
    'name' => 'filename.pdf',
    'message' => 'file not found',
]

注意以下内容:在HTML响应中将会删除/api部分,email-id是邮件的idattachment-key是附件的顺序,从0开始。

从5.1.0升级到5.2.0 - 破坏性更改

将以下参数添加到config/email_log.php数组末尾

    ...

    'routes_webhook_prefix' => env('EMAIL_LOG_ROUTES_WEBHOOK_PREFIX', env('EMAIL_LOG_ROUTES_PREFIX','')),
    'mailgun' => [
        'secret' => env('MAILGUN_SECRET', null),
        'filter_unknown_emails' => env('EMAIL_LOG_MAILGUN_FILTER_UNKNOWN_EMAILS', true),
    ],

从5.0.3升级到5.1.0 - 破坏性更改

重要 - 请立即升级到5.2.1,因为5.1.0升级中存在一些修复。我匆忙进行升级时遗漏了一些问题,这些问题在5.2.1中已得到纠正。

由于邮件日志附件可能会迅速增长到很大的尺寸,您可能希望使用一些外部存储来保存它们。为了启用此功能,我们需要利用Laravel的文件系统。如果您正在使用5.0.3并希望升级到5.1.0,请按照以下指南操作。

config/email_log.php文件中的一行从

'folder' => env('EMAIL_LOG_ATTACHMENT_FOLDER','email_log_attachments'),

修改为

'disk' => env('EMAIL_LOG_DISK','email_log_attachments'),

config/filesystems.php中添加

'email_log_attachments' => [
    'driver' => 'local',
    'root' => storage_path('app/email_log_attachments'),
],

'root'必须指向您之前保存附件的文件夹。

您还需要从email_log.attachments列中删除当前的名称前缀(默认为email_log_attachments)。例如,email_log_attachments/12345678910/my_file.jpg应重命名为12345678910/my_file.jpg

您可以使用php artisan tinker运行以下代码来修复这些问题。根据数据量的大小,完成可能需要一些时间。

$log = Yhw\LaravelEmailDatabaseLog\EmailLog::where('attachments','!=', null)
$log->count()
$log->chunk(100, function($chunk) { foreach($chunk as $l) { $l->attachments = str_replace('email_log_attachments/', '', $l->attachments); $l->save(); } })