dmcbbrn/laravel-email-database-log

一个简单的数据库日志器,用于记录由Laravel网站发送的所有外部邮件 - 基于 shvetsgroup/laravel-email-database-log 构建。

5.3.2 2023-03-20 23:23 UTC

README

一个简单的数据库日志器,用于记录由Laravel网站发送的所有外部邮件。从ShvetsGroup\LaravelEmailDatabaseLog分支而来。

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

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

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

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

版本5.1使用文件系统,这是与5.3的破坏性变化。以下为升级信息。

安装

步骤1:Composer

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

composer require dmcbrn/laravel-email-database-log

步骤2:配置

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

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

步骤3:迁移

现在,在终端中运行以下命令

php artisan migrate

步骤4:配置

要发布配置文件,请在终端中运行此命令

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

配置包含以下参数

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

//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磁盘。可以通过发布配置文件并更改'文件夹'值来更改email_log_attachments。

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

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

如果您想处理记录的电子邮件或在对系统进行一些附加数据保存/格式化,您可以通过Laravel监听器连接到Dmcbrn\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添加前缀。

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

Mailgun网关

您可以使用Mailgun网关来记录网关事件。在Mailgun网关部分添加以下内容

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端点可用,破坏性更改

API端点可用

带有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',
]

注意以下内容:/api 部分将在 HTML 响应中被删除,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 = Dmcbrn\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(); } })