apps-inteligentes / email-tracking
Requires
- php: ^8.1.0
- illuminate/contracts: ^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.9.2
- spatie/laravel-permission: ^3.0|^4.0|^5.0|^6.0
Requires (Dev)
- laravel/pint: ^1.17
- nunomaduro/collision: ^6.0|^7.0|^8.1
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0|^8.0|^9.0
- pestphp/pest: ^v2.13.0
- pestphp/pest-plugin-laravel: ^v2.3.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5|^10.0
- dev-main
- v5.3.0
- v5.2.0
- v5.1.1
- v5.1.0
- v5.0.1
- v5.0.0
- v4.0.2
- v4.0.1
- 3.0.0
- 2.2.0
- 2.1.0
- 2.0.0
- 1.2.2
- 1.2.0
- 1.1.0
- 1.0.2.x-dev
- 1.0.2
- v1.0.1.x-dev
- 1.0.1
- 1.0
- dev-blank_line_if
- dev-line_break
- dev-fix-publish-config-file
- dev-fix-middleware
- dev-fix-middlware
- dev-dependabot/github_actions/dependabot/fetch-metadata-2.2.0
- dev-remove-laravel-nova
- dev-henryavila-patch-1
- dev-laravel-11
- dev-log-email-body
- dev-fix#1
- dev-model-connection
This package is auto-updated.
Last update: 2024-09-06 15:41:36 UTC
README
使用 Laravel 跟踪电子邮件
放弃 Laravel Nova
自从我放弃 Laravel Nova 并转向 Filament 以来,此包将不再支持 Laravel Nova。此包中有关 Laravel Nova 的确切内容已移动到新的包 https://packagist.org.cn/packages/henryavila/laravel-nova-email-tracking 如果您正在使用 Laravel Nova,请使用此新包。
Mailgun 配置
在 Mailgun 界面上,将 webhook
添加到 URL <APP_URL>/webhooks/mailgun
安装
在 https://laravel.net.cn/docs/master/mail#mailgun-driver 上设置 Laravel Mail 与 Mailgun
在您的 .env
文件中定义环境变量
MAIL_MAILER=mailgun
MAILGUN_DOMAIN=yourdomain.com
MAILGUN_SECRET=key-99999999999999999999999999999999
通过 composer 安装包
composer require henryavila/email-tracking
使用以下命令发布并运行迁移
php artisan vendor:publish --tag="email-tracking-migrations"
php artisan migrate
使用以下命令发布配置文件
php artisan vendor:publish --tag="email-tracking-config"
这是发布配置文件的内容
return [ /** * if defined, the Email model will use this database connection. * This connection name must be defined in database.connections config file */ 'email-db-connection' => null, /** * Save the HTML Body of all sent messages */ 'log-body-html' => true, /** * Save the TXT Body of all sent messages */ 'log-body-txt' => true, ];
配置
在所有可以发送电子邮件的模型上添加 trait ModelWithEmailsSenderTrait
对于 Laravel 10,在 EventServiceProvider.php
文件中添加以下代码
protected $listen = [ \Illuminate\Mail\Events\MessageSent::class => [ \HenryAvila\EmailTracking\Listeners\LogEmailSentListener::class, ], ];
对于 Laravel 11,在 AppServiceProvider.php
文件的 boot()
方法中添加以下代码
public function boot(): void { // ... \Illuminate\Support\Facades\Event::listen( events: \Illuminate\Mail\Events\MessageSent::class, listener: \HenryAvila\EmailTracking\Listeners\LogEmailSentListener::class ); }
此时,从应用程序发送的所有电子邮件都将被记录在应用程序中,但发送者将不会保存
保存电子邮件发送者
要跟踪电子邮件发送者,您必须创建一个自定义 Mailable
或 Notification
。
Mailable
当创建新的 Mailable 时,它必须扩展类 HenryAvila\EmailTracking\Mail\TrackableMail
此外,您必须更改构造函数和内容函数。
这是默认邮件类
class SampleMail extends \Illuminate\Mail\Mailable { public function __construct() { // } public function content(): Content { return new Content( view: 'view.name', ); } }
将类更改为以下内容
class SampleMail extends \HenryAvila\EmailTracking\Mail\TrackableMail { public function __construct($modelSender) { $viewData = []; parent::__construct($modelSender, 'view.name', $viewData]); } }
要发送 Mailable,只需将模型传递给 Mailable 构造函数即可
// example: Send the Sample Mail to User with id 1 $user = User::find(1); Mail::to($user)->send(new App\Mail\SampleMail($user));
Notification
当创建通知时,您只需更改 toMail()
方法。用以下代码替换默认代码
public function toMail($notifiable): MailMessage { return (new MailMessage) ->line('The introduction to the notification.') ->action('Notification Action', url('/')) ->line('Thank you for using our application!'); }
用以下代码替换
public function __construct(protected \Illuminate\Database\Eloquent\Model $model) { // } public function toMail($notifiable): MailMessage { return (new \HenryAvila\EmailTracking\Notifications\TrackableNotificationMailMessage($this->model)) ->line('The introduction to the notification.') ->blankLine() ->line('Another line, after a blank line') ->blankLineIf($condition) ->action('Notification Action', url('/')) ->line('Thank you for using our application!'); }
要发送通知
// User with id 1 send the sample notification to multiple $clientes $user = User::find(1); Notification::send($clientes, new SampleNotification($user));
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
有关如何报告安全漏洞,请参阅我们的安全策略 our security policy
鸣谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 License File