PHPackages                             jlorente/laravel-sendgrid - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. jlorente/laravel-sendgrid

ActiveLibrary

jlorente/laravel-sendgrid
=========================

Laravel integration for the SendGrid php sdk.

1.0.4(3y ago)04.0k↓40%2BSD-3-ClausePHPPHP &gt;=7.1.3

Since Jun 16Pushed 3y ago1 watchersCompare

[ Source](https://github.com/jlorente/laravel-sendgrid)[ Packagist](https://packagist.org/packages/jlorente/laravel-sendgrid)[ RSS](/packages/jlorente-laravel-sendgrid/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (6)Used By (0)

SendGrid SDK for Laravel
========================

[](#sendgrid-sdk-for-laravel)

Laravel integration for the [SendGrid PHP SDK](https://github.com/sendgrid/sendgrid-php) including a notification channel.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

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
-------------

[](#configuration)

1. 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,
    ];
];
```

2. Add the following facade to the $aliases section.

config/app.php

```
return [
    //other stuff
    'aliases' => [
        //other stuff
        'SendGrid' => \Jlorente\Laravel\SendGrid\Facades\SendGrid::class,
    ];
];
```

3. Publish the package configuration file.

```
$ php artisan vendor:publish --provider='Jlorente\Laravel\SendGrid\SendGridServiceProvider'
```

4. 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=

```

Usage
-----

[](#usage)

You can use the facade alias SendGrid to execute api calls. The authentication params will be automaticaly injected.

```
SendGrid::send($mail);
```

Notification Channels
---------------------

[](#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](https://laravel.com/docs/5.6/notifications).

### SendGridEmailChannel

[](#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

[](#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:

```
