s-ichikawa / laravel-sendgrid-driver
This library adds a 'sendgrid' mail driver to Laravel.
Fund package maintenance!
s-ichikawa
Installs: 6 880 242
Dependents: 3
Suggesters: 0
Security: 0
Stars: 391
Watchers: 8
Forks: 91
Open Issues: 9
Requires
- guzzlehttp/guzzle: ^7.2
- illuminate/mail: ^9.0||^10.0||^11.0
- illuminate/notifications: ^9.0||^10.0||^11.0
- illuminate/support: ^9.0||^10.0||^11.0
Requires (Dev)
- illuminate/container: ^9.0||^10.0||^11.0
- illuminate/filesystem: ^9.0||^10.0||^11.0
- laravel/helpers: ^1.2
- phpunit/phpunit: ^9.5.8||^10.0
- vlucas/phpdotenv: ^5.4.1
Suggests
- s-ichikawa/sendgrid-api-builder: support to build json for sendgrid api
- dev-master
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.x-dev
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
- dev-dev-laravel-10-11
- dev-add-github-templates
- dev-for_laravel9
- dev-fix-str_random
- dev-1x-delete-validate-for-v2
- dev-2x-delete-validate-for-v2
- dev-delete-validate-for-v2
- dev-v2-guzzle7
This package is auto-updated.
Last update: 2024-11-02 14:44:12 UTC
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
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'), ]); }