salamwaddah / laravel-mandrill-driver
Mandrill 通知通道,支持 Laravel 5, 6, 7, 8, 9, 10, 11
v1.3.1
2024-04-02 12:59 UTC
Requires
- php: >=7.0.0
- guzzlehttp/guzzle: ^6.5 || ^7.0
- illuminate/notifications: ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
README
安装
composer require salamwaddah/laravel-mandrill-driver
配置
将以下内容添加到您的 .env 文件中
MANDRILL_SECRET=YOUR_MANDRILL_API_KEY
在您的 mail.php
文件中
'from' => [
'address' => 'noreply@example.com',
'name' => "From Name"
],
'mandrill' => [
'key' => env('MANDRILL_SECRET', 'SUPER SECRET KEY')
]
使用方法
基本用法
public function via($notifiable) { return [MandrillChannel::class]; } public function toMandrill($notifiable) { return (new MandrillMessage()) ->subject('Purchase successful') ->addTo($notifiable->email) ->view('mandrill-template-name', [ 'product' => $this->product->toArray(), 'user' => [ 'name' => $notifiable->name, 'phone' => $notifiable->phone ] ]); }
高级用法
public function toMandrill($notifiable) { return (new MandrillMessage()) ->subject('Purchase successful') ->templateName('mandrill-template-name') ->addTo($notifiable->email) ->addTos(['a@example.com', 'b@example.com']) ->fromName('Customized From') ->fromEmail('custom_from@example.com') ->replyTo('reply@example.com') ->content([ 'product' => $this->product->toArray(), ]); }
可用方法
注意:为了保持与 laravel 的 Mail
实现 replyTo
的一致性,您可以传递两个参数,第二个参数将被忽略,如果多次调用 replyTo
,则只使用第一个并忽略其他,因为 Mandrill 只允许一个回复邮箱地址。
Mandrill 中的用法(动态 Handlebars)
当您在 content
或 view
方法中指定内容时,您可以在 Mandrill 模板中使用 handlebars 语法,如下所示;
嘿 {{user.name}}
,您已成功购买 {{product.name}}
。
Mailchimp 语法
如果您想使用 Mailchimp 合并标签 而不是动态 Handlebars,则可以将 templateName
方法中的 $mergeLanguage
可选参数设置为 mailchimp
。
在 Mailchimp 合并标签中,不支持数组,因此每个标签只接受一个字符串。完整文档,包括 Mandrill 中的预定关键词
Mailchimp 示例
public function toMandrill($notifiable) { return (new MandrillMessage()) ->subject('Purchase successful') ->templateName('mandrill-template-name', 'mailchimp') << HERE ->addTo($notifiable->email) ->content([ 'customer_name' => $notifiable->name, 'invoice_link' => 'http://example.com/download/invoice.pdf', ]) }
然后在您的 Mandrill 模板中使用如下;
嗨 *|customer_name|*
,您可以从这里下载您的发票 *|invoice_link|*
。