mailersend/laravel-driver

MailerSend Laravel 驱动器

v2.7.0 2024-08-28 08:14 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,
                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'),
            );
    }
}

新语法

这是一个使用新的mailable语法发送电子邮件的示例。

app/Mail/TestEmail.php

<?php

namespace App\Mail;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use MailerSend\Helpers\Builder\Personalization;
use MailerSend\Helpers\Builder\Variable;
use MailerSend\LaravelDriver\MailerSendTrait;

class TestEmail extends Mailable
{
    use Queueable, SerializesModels, MailerSendTrait;

    /**
     * Create a new message instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Test Email',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        $to = Arr::get($this->to, '0.address');

        // Additional options for MailerSend API features
        $this->mailersend(
            template_id: null,
            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'),
        );

        return new Content(
            view: 'emails.test_html',
            text: 'emails.test_text'
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [
            Attachment::fromStorageDisk('public', 'example.png')
        ];
    }
}

我们提供了一个MailerSendTrait特质,它将mailersend方法添加到mailable,并允许您使用通过我们的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

许可证

The MIT License (MIT)