Coder Social home page Coder Social logo

laravel-sendgrid-driver's Introduction

Laravel SendGrid Driver

SensioLabsInsight Build Status

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.

Notification

If your project using guzzlehttp/guzzle 6.2.0 or less, you can use version 1.0.0 But the old version has security issues,

Install (Laravel5.1~)

Add the package to your composer.json and run composer update.

"require": {
    "s-ichikawa/laravel-sendgrid-driver": "^1.2"
},

or installed with composer

$ composer require s-ichikawa/laravel-sendgrid-driver

Add the sendgrid service provider in config/app.php:

'providers' => [
    Sichikawa\LaravelSendgridDriver\SendgridTransportServiceProvider::class,
];

Install (Laravel5.0)

Add the package to your composer.json and run composer update.

"require": {
    "s-ichikawa/laravel-sendgrid-driver": "5.0.x-dev"
},

or installed with composer

$ composer require s-ichikawa/laravel-sendgrid-driver:5.0.x-dev

Remove the default service provider and add the sendgrid service provider in config/app.php:

'providers' => [
//  'Illuminate\Mail\MailServiceProvider',

    'Sichikawa\LaravelSendgridDriver\MailServiceProvider',
];

Install (Lumen)

Add the package to your composer.json and run composer update.

"require": {
    "s-ichikawa/laravel-sendgrid-driver": "^1.1"
},

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'),
];

API v3

Configure

.env

MAIL_DRIVER=sendgrid
SENDGRID_API_KEY='YOUR_SENDGRID_API_KEY'

config/services.php (In using lumen, require creating config directory and file.)

    'sendgrid' => [
        'api_key' => env('SENDGRID_API_KEY'),
        'version' => 'v3',
    ],

Request Body Parameters

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

\Mail::send('view', $data, function (Message $message) {
    $message
        ->to('[email protected]', 'foo_name')
        ->from('[email protected]', 'bar_name')
        ->embedData([
            'categories' => ['user_group1'],
            'send_at' => $send_at->getTimestamp(),
        ], 'sendgrid/x-smtpapi');
});

more info https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html#-Request-Body-Parameters

API v2

Configure

.env

MAIL_DRIVER=sendgrid
SENDGRID_API_KEY='YOUR_SENDGRID_API_KEY'

config/services.php (In using lumen, require creating config directory and file.)

    'sendgrid' => [
        'api_key' => env('SENDGRID_API_KEY')
    ],

Use SMTP API

Sendgrid's SMTP API is a very handy feature.

To use this 'sendgrid/x-smtpapi' functionality, use our embedData() function.

API v2

\Mail::send('view', $data, function (Message $message) {
    $message
        ->to('[email protected]', 'foo_name')
        ->from('[email protected]', 'bar_name')
        ->embedData([
            'to' => ['[email protected]', '[email protected]'],
            'sub' => [
                '-email-' => ['[email protected]', '[email protected]'],
            ],
            'category' => 'user_group1',
            'unique_args' => [
                'user_id' => 123
            ]
        ], 'sendgrid/x-smtpapi');
});

API v3

\Mail::send('view', $data, function (Message $message) {
    $message
        ->to('[email protected]', 'foo_name')
        ->from('[email protected]', 'bar_name')
        ->replyTo('[email protected]', 'foobar');
        ->embedData([
            'personalizations' => [
                [
                    'to' => [
                        'email' => '[email protected]',
                        'name' => 'user1',
                    ],
                    'substitutions' => [
                        '-email-' => '[email protected]',
                    ],
                ],
                [
                    'to' => [
                        'email' => '[email protected]',
                        'name' => 'user2',
                    ],
                    'substitutions' => [
                        '-email-' => '[email protected]',
                    ],
                ],
            ],
            'categories' => ['user_group1'],
            'custom_args' => [
                'user_id' => "123" // Make sure this is a string value
            ]
        ], 'sendgrid/x-smtpapi');
});
  • custom_args values have to be strings. Sendgrid API gives a non-descriptive error message when you enter non-string values.

Difference v2 vs v3

Have a look at 'How to migrate' for more information on the difference in parameters.

Use in Mailable

<?
use Sichikawa\LaravelSendgridDriver\SendGrid;

class SendGridSample extends Mailable
{
    use SendGrid;
    
    public function build()
    {
        return $this
            ->view('template name')
            ->subject('subject')
            ->from('[email protected]')
            ->to(['[email protected]'])
            ->sendgrid([
                'personalizations' => [
                    [
                        'substitutions' => [
                            ':myname' => 's-ichikawa',
                        ],
                    ],
                ],
            ]);
    }
}

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.