s-ichikawa/laravel-sendgrid-driver
Composer 安装命令:
composer require s-ichikawa/laravel-sendgrid-driver
包简介
This library adds a 'sendgrid' mail driver to Laravel.
README 文档
README
A Mail Driver with support for Sendgrid Web API, using the original Laravel API. This library extends the original Laravel classes, so it uses exactly the same methods.
To use this package required your Sendgrid Api Key. Please make it Here.
Compatibility
| Laravel | laravel-sendgrid-driver |
|---|---|
| 9, 10, 11 | ^4.0 |
| 7, 8 | ^3.0 |
| 5, 6 | ^2.0 |
Install (for Laravel)
Add the package to your composer.json and run composer update.
"require": { "s-ichikawa/laravel-sendgrid-driver": "^4.0" },
or installed with composer
$ composer require s-ichikawa/laravel-sendgrid-driver
Install (for Lumen)
Add the package to your composer.json and run composer update.
"require": { "s-ichikawa/laravel-sendgrid-driver": "^4.0" },
or installed with composer
$ composer require "s-ichikawa/laravel-sendgrid-driver"
Add the sendgrid service provider in bootstrap/app.php
$app->configure('mail'); $app->configure('services'); $app->register(Sichikawa\LaravelSendgridDriver\MailServiceProvider::class); unset($app->availableBindings['mailer']);
Create mail config files. config/mail.php
<?php return [ 'driver' => env('MAIL_DRIVER', 'sendgrid'), ];
Configure
.env
MAIL_DRIVER=sendgrid
SENDGRID_API_KEY='YOUR_SENDGRID_API_KEY'
# Optional: for 7+ laravel projects
MAIL_MAILER=sendgrid
config/services.php (In using lumen, require creating config directory and file.)
'sendgrid' => [ 'api_key' => env('SENDGRID_API_KEY'), ],
config/mail.php
'mailers' => [ 'sendgrid' => [ 'transport' => 'sendgrid', ], ],
endpoint config
If you need to set custom endpoint, you can set any endpoint by using endpoint key.
For example, calls to SendGrid API through a proxy, call endpoint for confirming a request.
'sendgrid' => [ 'api_key' => env('SENDGRID_API_KEY'), 'endpoint' => 'https://custom.example.com/send', ],
How to use
Every request made to /v3/mail/send will require a request body formatted in JSON containing your email’s content and metadata. Required parameters are set by Laravel's usually mail sending, but you can also use useful features like "categories" and "send_at".
more info 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'], ]); } }
Using Template Id
Illuminate\Mailer has generally required a view file. But in case of using template id, set an empty array at view function.
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'), ]); }
s-ichikawa/laravel-sendgrid-driver 适用场景与选型建议
s-ichikawa/laravel-sendgrid-driver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.9M 次下载、GitHub Stars 达 408, 最近一次更新时间为 2016 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「sendgrid」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 s-ichikawa/laravel-sendgrid-driver 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 s-ichikawa/laravel-sendgrid-driver 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 s-ichikawa/laravel-sendgrid-driver 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SendGrid plugin for CakePHP 4 - Send emails using SendGrid API
A collection of monolog handlers using PSR-18 http client
Componente para integração com serviços de mensageria com APIs externas como Twilio, SendGrid, AWS SES, etc.
A drop-in mailer replacement for SendGrid
PHP 8.1+ Sendgrid integration for the Yii framework
Alfabank REST API integration
统计信息
- 总下载量: 9.9M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 413
- 点击次数: 28
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-24