mdhesari/laravel-driver

MailerSend Laravel 驱动程序

v2.2.1 2022-12-22 12:06 UTC

This package is auto-updated.

Last update: 2024-09-22 15:41:29 UTC


README

MailerSend Laravel 驱动程序

MIT licensed

目录

安装

要求

  • Laravel 9.0+
  • PHP 8.0+
  • Guzzle 7.0+
  • mailersend.com 获取一个 API 密钥

对于 Laravel 7.x - 8.x 的支持,请查看 1.x 分支

设置

您可以通过 composer 安装此包

composer require mailersend/laravel-driver

之后,您需要在 .env 文件中设置 MAILERSEND_API_KEY

MAILERSEND_API_KEY=

config/mail.php 中的 mailers 数组中将 MailerSend 添加为 Laravel Mailer

'mailersend' => [
    'transport' => 'mailersend',
],

并在 .env 文件中设置环境变量 MAIL_MAILER

MAIL_MAILER=mailersend

另外,请确保您的 FROM 数据在 .env 中填写完整

MAIL_FROM_ADDRESS=app@yourdomain.com
MAIL_FROM_NAME="App Name"

用法

这是一个示例 mailable,您可以使用它来发送电子邮件。

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()
    {
        // Recipient for use with variables and/or personalization
        $to = Arr::get($this->to, '0.address');

        return $this
            ->view('emails.test_html')
            ->text('emails.test_text')
            ->attachFromStorageDisk('public', 'example.png')
            // Additional options for MailerSend API features
            ->mailersend(
                template_id: null,
                variables: [
                    new Variable($to, ['name' => 'Your Name'])
                ],
                tags: ['tag'],
                personalization: [
                    new Personalization($to, [
                        'var' => 'variable',
                        'number' => 123,
                        'object' => [
                            'key' => 'object-value'
                        ],
                        'objectCollection' => [
                            [
                                'name' => 'John'
                            ],
                            [
                                'name' => 'Patrick'
                            ]
                        ],
                    ])
                ],
                precedenceBulkHeader: true,
                sendAt: new Carbon('2022-01-28 11:53:20'),
            );
    }
}

我们提供了一个 MailerSendTrait 特性,它为 mailable 添加了 mailersend 方法,并允许您使用我们 API 中可用的附加选项。

创建 mailable 后,您可以使用以下方式发送它

use App\Mail\TestEmail;
use Illuminate\Support\Facades\Mail;

Mail::to('recipient@domain.com')
    ->cc('cc@domain.com')
    ->bcc('bcc@domain.com')
    ->send(new TestEmail());

有关更多信息,请参阅 Laravel Mail 文档MailerSend API 文档

支持和反馈

如果您发现任何错误,请直接在 GitHub 中提交问题。

如果您在使用我们的驱动程序时遇到任何问题,请通过电子邮件 info@mailersend.com 联系我们的支持。

官方 API 文档位于 https://developers.mailersend.com

许可证

MIT 许可证 (MIT)