PHP code example of swiftmade / laravel-sendgrid-notification-channel

1. Go to this page and download the library: Download swiftmade/laravel-sendgrid-notification-channel 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/ */

    

swiftmade / laravel-sendgrid-notification-channel example snippets


return [

    // other services...

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



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->routeNotificationFor('mail')
             * ->to('[email protected]', 'Mr. Smith')
             */
            ->payload([
                "template_var_1" => "template_value_1"
            ]);
	}
}


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

use SendGrid\Mail\Mail;

return (new SendGridMessage('Your SendGrid template ID'))
    ->payload([
        'template_var_1' => 'template_value_1',
        'template_var_2' => [
            'value_1',
            'value_2',
            'value_3',
        ],
    ])
    ->customize(function (Mail $mail) {
        // Send a carbon copy (cc) to another address
        $mail->addCc('[email protected]');
        // Send a blind carbon copy (bcc) to another address
        $mail->addBcc('[email protected]');
    });

use Illuminate\Notifications\Events\NotificationSent;

Event::listen(NotificationSent::class, function (NotificationSent $event) {
    /**
     * @var \SendGrid\Response $response
     */
    $response = $event->response;
});