Giter Site home page Giter Site logo

ricuss / laravel-sendgrid-notification-channel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from swiftmade/laravel-sendgrid-notification-channel

0.0 0.0 0.0 52 KB

Sendgrid driver for Laravel notifications. Supports dynamic templates.

License: MIT License

PHP 100.00%

laravel-sendgrid-notification-channel's Introduction

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('[email protected]', 'App name')
             */
            /**
             * optionally set the recipient.
             * by default it's $notifiable->email:
             * ->to('[email protected]', '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');

Enabling Sandbox Mode

To enable sandbox mode you will need to chain on the enableSandboxMode() to the message object.

Example:

return (new SendGridMessage('Your SendGrid template ID'))
    ->enableSandboxMode()
    ->payload([
        "template_var_1" => "template_value_1"
    ]);

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 [email protected] 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.

laravel-sendgrid-notification-channel's People

Contributors

aozisik avatar atymic avatar casperboone avatar freekmurze avatar irazasyed avatar jaybizzle avatar lukewaite avatar mpociot avatar ricuss avatar semihcosu avatar talvbansal avatar themsaid avatar zbrody avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.