jlorente / laravel-sendgrid
Laravel integration for the SendGrid php sdk.
Installs: 3 084
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 2
Open Issues: 0
Requires
- php: >=7.1.3
- illuminate/support: >=5.6
- sendgrid/sendgrid: ~7
README
Laravel integration for the SendGrid PHP SDK including a notification channel.
Installation
The preferred way to install this extension is through composer.
With Composer installed, you can then install the extension using the following commands:
$ php composer.phar require jlorente/laravel-sendgrid
or add
... "require": { "jlorente/laravel-sendgrid": "*" }
to the require
section of your composer.json
file.
Configuration
- Register the ServiceProvider in your config/app.php service provider list.
config/app.php
return [ //other stuff 'providers' => [ //other stuff \Jlorente\Laravel\SendGrid\SendGridServiceProvider::class, ]; ];
- Add the following facade to the $aliases section.
config/app.php
return [ //other stuff 'aliases' => [ //other stuff 'SendGrid' => \Jlorente\Laravel\SendGrid\Facades\SendGrid::class, ]; ];
- Publish the package configuration file.
$ php artisan vendor:publish --provider='Jlorente\Laravel\SendGrid\SendGridServiceProvider'
- Set the api_key in the config/sendgrid.php file or use the predefined env variables.
config/sendgrid.php
return [ 'api_key' => 'YOUR_API_KEY', //other configuration ];
or .env
//other configurations
SENDGRID_API_KEY=<YOUR_API_KEY>
Usage
You can use the facade alias SendGrid to execute api calls. The authentication params will be automaticaly injected.
SendGrid::send($mail);
Notification Channels
A notification channel is included in this package and allow you to integrate the SendGrid send email service.
You can find more info about Laravel notifications in this page.
SendGridEmailChannel
If you want to send an email through SendGrid, you should define a toSendGrid method on the notification class. This method will receive a $notifiable entity and should return a \SendGrid\Mail\Mail instance.
/** * Get the Mail object instance. * * @param mixed $notifiable * @return \SendGrid\Mail\Mail */ public function toSendGrid($notifiable) { $mail = new \SendGrid\Mail\Mail(); $mail->setFrom(); $mail->setTemplateId('d-4n23blaasjdgdg3242'); $mail->addDynamicTemplateData('username', 'John'); return $mail; }
Once done, you must add the notification channel in the array of the via() method of the notification:
/** * Get the notification channels. * * @param mixed $notifiable * @return array|string */ public function via($notifiable) { return [SendGridEmailChannel::class]; }
Routing the Notifications
When sending notifications via SendGrid channel, the notification system will automatically look for an email attribute on the notifiable entity. If you would like to customize the number you should define a routeNotificationForSendGrid method on the entity:
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * Route notifications for the SendGrid SMS channel. * * @param \Illuminate\Notifications\Notification $notification * @return string */ public function routeNotificationForSendGrid($notification) { return $this->email; } }
License
Copyright © 2021 José Lorente Martín jose.lorente.martin@gmail.com.
Licensed under the BSD 3-Clause License. See LICENSE.txt for details.