ichikawa/laravel-sendgrid-driver

这个库为Laravel添加了一个'Sendgrid'邮件驱动。

4.0.5 2024-03-15 01:27 UTC

README

SymfonyInsight Build Status

支持Sendgrid Web API的邮件驱动,使用原始Laravel API。此库扩展了原始Laravel类,因此使用完全相同的方法。

使用此包需要您的Sendgrid API密钥。请将其添加到此处

兼容性

安装(对于Laravel

将包添加到composer.json,并运行composer update。

"require": {
    "s-ichikawa/laravel-sendgrid-driver": "^4.0"
},

或使用composer安装

$ composer require s-ichikawa/laravel-sendgrid-driver

安装(对于Lumen

将包添加到composer.json,并运行composer update。

"require": {
    "s-ichikawa/laravel-sendgrid-driver": "^4.0"
},

或使用composer安装

$ composer require "s-ichikawa/laravel-sendgrid-driver"

在bootstrap/app.php中添加sendgrid服务提供者

$app->configure('mail');
$app->configure('services');
$app->register(Sichikawa\LaravelSendgridDriver\MailServiceProvider::class);

unset($app->availableBindings['mailer']);

创建邮件配置文件。config/mail.php

<?php
return [
    'driver' => env('MAIL_DRIVER', 'sendgrid'),
];

配置

.env

MAIL_DRIVER=sendgrid
SENDGRID_API_KEY='YOUR_SENDGRID_API_KEY'
# Optional: for 7+ laravel projects
MAIL_MAILER=sendgrid 

config/services.php(在Lumen中使用时,需要创建配置目录和文件。)

    'sendgrid' => [
        'api_key' => env('SENDGRID_API_KEY'),
    ],

config/mail.php

    'mailers' => [
        'sendgrid' => [
            'transport' => 'sendgrid',
        ],
    ],

端点配置

如果您需要设置自定义端点,您可以使用endpoint键设置任何端点。例如,通过代理调用SendGrid API,调用确认请求的端点。

    'sendgrid' => [
        'api_key' => env('SENDGRID_API_KEY'),
        'endpoint' => 'https://custom.example.com/send',
    ],

如何使用

对/v3/mail/send发出的每个请求都需要一个JSON格式的请求体,包含您的邮件内容和元数据。通常由Laravel的邮件发送设置设置所需参数,但您也可以使用诸如"categories"和"send_at"之类的有用功能。

更多信息https://www.twilio.com/docs/sendgrid/api-reference/mail-send/mail-send

Laravel 10, 11

<?
use Sichikawa\LaravelSendgridDriver\SendGrid;

class SendGridSample extends Mailable
{
    use SendGrid;
    
    public function envelope(): Envelope
    {
        $this->sendgrid([
                'personalizations' => [
                    [
                        'to' => [
                            ['email' => 'to1@gmail.com', 'name' => 'to1'],
                            ['email' => 'to2@gmail.com', 'name' => 'to2'],
                        ],
                        'cc' => [
                            ['email' => 'cc1@gmail.com', 'name' => 'cc1'],
                            ['email' => 'cc2@gmail.com', 'name' => 'cc2'],
                        ],
                        'bcc' => [
                            ['email' => 'bcc1@gmail.com', 'name' => 'bcc1'],
                            ['email' => 'bcc2@gmail.com', 'name' => 'bcc2'],
                        ],
                    ],
                ],
                'categories' => ['user_group1'],
            ]);
        return new Envelope(
            from:    'from@example.com',
            replyTo: 'reply@example.com',
            subject: 'example',
        );
    }
}

Laravel 9

<?
use Sichikawa\LaravelSendgridDriver\SendGrid;

class SendGridSample extends Mailable
{
    use SendGrid;
    
    public function build():
    {
        return $this
            ->view('template name')
            ->subject('subject')
            ->from('from@example.com')
            ->to(['to@example.com'])
            ->sendgrid([
                'personalizations' => [
                    [
                        'to' => [
                            ['email' => 'to1@gmail.com', 'name' => 'to1'],
                            ['email' => 'to2@gmail.com', 'name' => 'to2'],
                        ],
                        'cc' => [
                            ['email' => 'cc1@gmail.com', 'name' => 'cc1'],
                            ['email' => 'cc2@gmail.com', 'name' => 'cc2'],
                        ],
                        'bcc' => [
                            ['email' => 'bcc1@gmail.com', 'name' => 'bcc1'],
                            ['email' => 'bcc2@gmail.com', 'name' => 'bcc2'],
                        ],
                    ],
                ],
                'categories' => ['user_group1'],
            ]);
    }
}

使用模板ID

Illuminate\Mailer通常需要一个视图文件。但在使用模板ID的情况下,请在视图函数中设置一个空数组。

Laravel 10, 11

<?
    public function envelope(): Envelope
    {
        $this->sendgrid([
            'personalizations' => [
                [
                    'dynamic_template_data' => [
                        'title' => 'Subject',
                        'name'  => 's-ichikawa',
                    ],
                ],
            ],
            'template_id' => config('services.sendgrid.templates.dynamic_template_id'),
        ]);
        return new Envelope(
            from:    'from@example.com',
            replyTo: 'reply@example.com',
            subject: 'example',
        );
    }

Laravel 9

<?
    public function build():
    {
        return $this
            ->view('template name')
            ->subject('subject')
            ->from('from@example.com')
            ->to(['to@example.com'])
            ->sendgrid([
                'personalizations' => [
                    [
                        'dynamic_template_data' => [
                            'title' => 'Subject',
                            'name'  => 's-ichikawa',
                        ],
                    ],
                ],
                'template_id' => config('services.sendgrid.templates.dynamic_template_id'),
            ]);
    }