Skip to content

timstl/laravel-sendgrid-notification-channel

 
 

Repository files navigation

Latest Version on Packagist Software License Total Downloads

Allows you to send Laravel notifications using Sendgrid's Dynamic Transactional Templates feature. Supports Laravel 7.x, 8.x and 9.x.

(For older versions of Laravel, install v1)

Contents

Installation

To get started, you need to require this package:

composer require swiftmade/laravel-sendgrid-notification-channel

The service provider will be auto-detected by Laravel. If you've turned auto-discovery off, add the following service provider in your config/app.php.

NotificationChannels\SendGrid\SendGridServiceProvider::class,

Next, make sure you have a valid Sendgrid API key in config/services.php. You may copy the example configuration below to get started:

return [

    // other services...

    // add this...
    'sendgrid' => [
        'api_key' => env('SENDGRID_API_KEY'),
    ],
];

Usage

To send an email using dynamic templates, you need to:

  1. Return SendGridChannel::class in the via() method. (Not mail)
  2. Add and implement the toSendGrid($notifiable){ } method.

Example:

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use NotificationChannels\SendGrid\SendGridChannel;

class ExampleNotification extends Notification
{
    public function via($notifiable)
    {
        return [
            SendGridChannel::class,
            // And any other channels you want can go here...
        ];
    }

    // ...

    public function toSendGrid($notifiable)
    {
        return (new SendGridMessage('Your SendGrid template ID'))
            /**
             * optionally set the from address.
             * by default this comes from config/mail.from
             * ->from('no-reply@test.com', 'App name')
             */
            /**
             * optionally set the recipient.
             * by default it's $notifiable->email:
             * ->to('hello@example.com', 'Mr. Smith')
             */
            ->payload([
                "template_var_1" => "template_value_1"
            ]);
	}
}

toSendGrid method will receive a $notifiable entity and should return a NotificationChannels\SendGrid\SendGridMessage instance.

💡 Unless you set it explicitly, the From address will be set to config('mail.from.address') and the To value will be what returns from $notifiable->routeNotificationFor('mail');

Sandbox Mode

The sandbox mode is off by default. You can use the setSandboxMode($bool) method to enable/disable it.

Example:

return (new SendGridMessage('Your SendGrid template ID'))
    ->setSandboxMode(true)
    ->payload([
        'template_var_1' => 'template_value_1',
        'template_var_2' => [
            'value_1',
            'value_2',
            'value_3',
        ],
    ]);

When making a request with sandbox mode enabled, Sendgrid will validate the form, type, and shape of your request. No email will be sent. You can read more about the sandbox mode here.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email hello@swiftmade.co instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Sendgrid driver for Laravel notifications. Supports dynamic templates.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%