devkea/laravel-mandrill-driver

Laravel 5, 6, 7, 8, 9 的 Mandrill 通知通道

1.3 2023-08-24 00:08 UTC

This package is not auto-updated.

Last update: 2024-09-20 04:10:24 UTC


README

源代码: https://github.com/salamwaddah/laravel-mandrill-driver

更新内容

  1. 添加附件;
  2. 指定动态 API 密钥。

安装

composer require devkea/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')
        ->attach($notifiable->file)
        ->content([
            'product' => $this->product->toArray(),
        ]);
}

可用方法

注意:为了与 Laravel 的 Mail 实现中的 replyTo 保持一致,您可以传递两个参数,第二个参数将被忽略,如果多次调用 replyTo,则只使用第一个并忽略其他,因为 Mandrill 只允许一个用于回复的电子邮件地址。

在 Mandrill 中的用法(动态 Handlebars)

当在 contentview 方法中指定您的内容时,您可以在 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|*