jestherthejoker / laravel-driver
MailerSend Laravel 驱动自定义库
v1.0.2
2021-10-30 16:20 UTC
Requires
- php: ^7.4|^8.0
- illuminate/support: ^7.0|^8.0
- mailersend/mailersend: ^0.3.1
- nyholm/psr7: ^1.3
- php-http/guzzle7-adapter: ^0.1
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.0
README
MailerSend Laravel 驱动
目录
安装
需求
- Laravel 7.0+
- PHP 7.4+
- Guzzle 7.0+
- 从mailersend.com获取 API 密钥
设置
您可以通过 composer 安装此包
composer require mailersend/laravel-driver
之后,您需要在您的 .env
文件中设置 MAILERSEND_API_KEY
MAILERSEND_API_KEY=
在 config/mail.php
的 mailers
数组中将 MailerSend 添加为 Laravel 邮件发送器
'mailersend' => [ 'transport' => 'mailersend', ],
并在您的 .env
文件中设置环境变量 MAIL_MAILER
MAIL_MAILER=mailersend
另外,请确保您的 FROM
数据已在 .env
中填写
MAIL_FROM_ADDRESS=app@yourdomain.com MAIL_FROM_NAME="App Name"
升级及 Guzzle 6 支持
从 v0.1 升级
如果您是从 v0.1
分支升级,请注意您需要将 Guzzle 升级至至少版本 7。请参阅官方指南获取更多信息。
使用方法
这是一个示例 邮件实体,您可以使用它来发送电子邮件。
app/Mail/TestEmail.php
namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Arr; use MailerSend\Helpers\Builder\Variable; use MailerSend\Helpers\Builder\Personalization; use MailerSend\LaravelDriver\MailerSendTrait; class TestEmail extends Mailable { use Queueable, SerializesModels, MailerSendTrait; public function build() { $to = Arr::get($this->to, '0.address'); return $this->view('emails.test_html') ->text('emails.test_text') ->attachFromStorageDisk('public', 'example.png') ->mailersend( // Template ID null, // Variables for simple personalization [ new Variable($to, ['name' => 'Your Name']) ], // Tags ['tag'], // Advanced personalization [ new Personalization($to, [ 'var' => 'variable', 'number' => 123, 'object' => [ 'key' => 'object-value' ], 'objectCollection' => [ [ 'name' => 'John' ], [ 'name' => 'Patrick' ] ], ]) ] ); } }
附件通过标准的 Laravel 方法添加。
我们提供了一个 MailerSendTrait
特性,它向邮件实体添加一个 mailersend
方法,并允许您使用通过我们的 API 可用的模板、变量和标签支持。
创建邮件实体后,您可以使用以下方法发送它
use App\Mail\TestEmail; use Illuminate\Support\Facades\Mail; Mail::to('recipient@domain.com')->send(new TestEmail());
有关更多信息,请参阅Laravel 邮件文档和MailerSend API 文档。
支持和反馈
如果您发现任何错误,请直接在此 GitHub 中提交问题。
如果您在使用我们的驱动器时遇到任何问题,请随时通过电子邮件 info@mailersend.com 联系我们的支持。
官方 API 文档在 https://developers.mailersend.com