seinoxygen / laravel-elastic-email
为 Elastic Email 提供的 Laravel 封装
Requires
- php: ^7.1|^8.0
- guzzlehttp/guzzle: ^6.3|^7.0
- laravel/framework: ^6.0|^7.0|^8.0|^9.0|^10.0
README
Laravel 封装,用于通过 Elastic Email 服务和 API 功能发送电子邮件,并允许您检查每封发送的电子邮件的状态。它提供了一个基本的电子邮件日志表,用于存储所有外发电子邮件,您可以通过它链接到一个模型。
安装
使用 composer CLI 将 Laravel Elastic Email 添加为依赖项
composer require seinoxygen/laravel-elastic-email
邮件服务使用
此包与 Laravel 的原生邮件发送器完全一样。请参阅 Laravel 的邮件文档。
将以下内容添加到您的 config/services.php,并将正确的值添加到您的 .env 文件中
'elastic_email' => [ 'key' => env('ELASTIC_KEY'), 'account' => env('ELASTIC_ACCOUNT') ]
将以下内容添加到您的 config/mail.php
'elastic_email' => [ 'transport' => 'elasticemail' ]
接下来,在 config/app.php 中,注释掉 Laravel 的默认 MailServiceProvider。如果使用 < Laravel 5.5,将 MailServiceProvider 和 ApiServiceProvider 添加到 providers 数组中
'providers' => [ ... SeinOxygen\ElasticEmail\MailServiceProvider::class, SeinOxygen\ElasticEmail\ApiServiceProvider::class, ... ],
接下来,在 config/app.php 中,将 ElasticEmail 添加到 aliases 数组中
'aliases' => [ ... 'ElasticEmail' => SeinOxygen\ElasticEmail\Facades\ElasticEmail::class, ... ],
最后,通过在 .env 文件中将 MAIL_DRIVER=elastic_email 设置为弹性电子邮件,将默认的邮件提供商切换到弹性电子邮件
外发电子邮件跟踪
要跟踪驱动程序发送的所有电子邮件,您需要发布迁移和配置文件
php artisan vendor:publish --provider="SeinOxygen\ElasticEmail\ApiServiceProvider" --tag="migrations"
php artisan migrate
php artisan vendor:publish --provider="SeinOxygen\ElasticEmail\ApiServiceProvider" --tag="config"
默认情况下,所有外发电子邮件都将存储在 Elastic Email 的 message_id 和 transaction_id 中。
有关更多信息,请检查 config/elasticemail.php。
将外发电子邮件链接到您的模型
在您的 mailable 中,请确保按照以下方式设置 with 数组。
public function build() { // You can set ad many models you want to relate with the outgoing email $models = [ [$yourmodel->id, get_class($yourmodel)], ]; return $this ->subject("My Subject") ->view('my-view') ->with([ 'models' => $models ]); }
如果看起来很丑,我还没有找到更好的方法...还没有。
捕获 Webhook 事件
您需要在 Elastic Email 服务中设置一个指向 yourappurl.com/webhook/elasticemail 的 webhook
当数据发送到 webhook url 时,会触发一个事件。
<?php namespace app\Listeners; use SeinOxygen\ElasticEmail\Events\WebhookCallReceived; class WebhookCallListerner { public function handle(WebhookCallReceived $event) { $request = $event->request; } }
API 使用
请访问https://api.elasticemail.com/public/help 以获取文档。
//For contact ElasticEmail::Contact() //For emails ElasticEmail::Email()
致谢
此包基于ZanySoft
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 以获取更多信息。