GithubHelp home page GithubHelp logo

tomcodes / laravel-sendgrid-driver Goto Github PK

View Code? Open in Web Editor NEW

This project forked from s-ichikawa/laravel-sendgrid-driver

0.0 0.0 0.0 164 KB

This library can add sendgrid driver into the laravel's mail configure.

License: MIT License

PHP 100.00%

laravel-sendgrid-driver's Introduction

Laravel SendGrid Driver

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

Install (for Laravel)

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

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

or installed with composer

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

Add the sendgrid service provider in config/app.php: (Laravel 5.5+ uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.)

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

Install (for Lumen)

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

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

or installed with composer

$ composer require "s-ichikawa/laravel-sendgrid-driver:^4.0"

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

Configure

.env

MAIL_DRIVER=sendgrid
SENDGRID_API_KEY='YOUR_SENDGRID_API_KEY'
# Optional: for 7+ laravel projects
MAIL_MAILER=sendgrid 

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

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

config/mail.php

    'mailers' => [
        'sendgrid' => [
            'transport' => 'sendgrid',
        ],
    ],

endpoint config

If you need to set custom endpoint, you can set any endpoint by using endpoint key. For example, calls to SendGrid API through a proxy, call endpoint for confirming a request.

    'sendgrid' => [
        'api_key' => env('SENDGRID_API_KEY'),
        'endpoint' => 'https://custom.example.com/send',
    ],

How to use

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

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

<?
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' => [
                    [
                        'to' => [
                            ['email' => '[email protected]', 'name' => 'to1'],
                            ['email' => '[email protected]', 'name' => 'to2'],
                        ],
                        'cc' => [
                            ['email' => '[email protected]', 'name' => 'cc1'],
                            ['email' => '[email protected]', 'name' => 'cc2'],
                        ],
                        'bcc' => [
                            ['email' => '[email protected]', 'name' => 'bcc1'],
                            ['email' => '[email protected]', 'name' => 'bcc2'],
                        ],
                    ],
                ],
                'categories' => ['user_group1'],
            ]);
    }
}

Using Template Id

Illuminate\Mailer has generally required a view file. But in case of using template id, set an empty array at view function.

<?
    public function build()
    {
        return $this
            ->view('template name')
            ->subject('subject')
            ->from('[email protected]')
            ->to(['[email protected]'])
            ->sendgrid([
            'personalizations' => [
                [
                    'dynamic_template_data' => [
                        'title' => 'Subject',
                        'name'  => 's-ichikawa',
                    ],
                ],
            ],
            'template_id' => config('services.sendgrid.templates.dynamic_template_id'),
        ]);
    }

laravel-sendgrid-driver's People

Contributors

amouillard avatar daisuke-fukuda avatar dmytro-skelia avatar elidrake avatar foo99 avatar gableroux avatar gstpereira avatar iandenh avatar jasonlbeggs avatar johnpbloch avatar l-alexandrov avatar lavatoaster avatar matt-allan avatar robertoandres24 avatar roerjo avatar s-ichikawa avatar sonnysantino avatar tomcodes avatar vrajroham avatar ziming 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.