PHP code example of s-ichikawa / laravel-sendgrid-driver

1. Go to this page and download the library: Download s-ichikawa/laravel-sendgrid-driver library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

s-ichikawa / laravel-sendgrid-driver example snippets


$app->configure('mail');
$app->configure('services');
$app->register(Sichikawa\LaravelSendgridDriver\MailServiceProvider::class);

unset($app->availableBindings['mailer']);


return [
    'driver' => env('MAIL_DRIVER', 'sendgrid'),
];

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

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

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

<?
use Sichikawa\LaravelSendgridDriver\SendGrid;

class SendGridSample extends Mailable
{
    use SendGrid;
    
    public function envelope(): Envelope
    {
        $this->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'],
            ]);
        return new Envelope(
            from:    '[email protected]',
            replyTo: '[email protected]',
            subject: 'example',
        );
    }
}

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

<?
    public function envelope(): Envelope
    {
        $this->sendgrid([
            'personalizations' => [
                [
                    'dynamic_template_data' => [
                        'title' => 'Subject',
                        'name'  => 's-ichikawa',
                    ],
                ],
            ],
            'template_id' => config('services.sendgrid.templates.dynamic_template_id'),
        ]);
        return new Envelope(
            from:    '[email protected]',
            replyTo: '[email protected]',
            subject: 'example',
        );
    }

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