Skip to content

jorisvaesen/cakephp-sendgrid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sendgrid wrapper for CakePHP

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require jorisvaesen/cakephp-sendgrid

Load the plugin in config/bootstrap.php:

Plugin::load('JorisVaesen/Sendgrid', ['bootstrap' => true]);

Configuration

Method 1 (using ENV)

The plugin loads from dotenv the following:

SENDGRID_API_KEY="your_sendgrid_api_key"

Method 2 (using app.php)

Set the sendgrid api key in app.php:

'Sendgrid' => [
    'apiKey' => 'your_sendgrid_api_key',
],

Method 3 (hardcoded)

Add in your app.php file, in the EmailTransport item

'EmailTransport' => [
        ...
        'sendgrid' => [
            'className' => '\JorisVaesen\Sendgrid\Mailer\Transport\SendgridTransport',
            'password' => 'your_sendgrid_api_key',
        ],
        ...
    ],

To use it by default, set your default transport to sendgrid in app.php:

'Email' => [
    'default' => [
        'transport' => 'sendgrid',
    ],
],

Callback

If you want to use Sendgrid API methods which do not have a CakePHP equivalent, you can make use of the callback configuration:

'EmailTransport' => [
        ...
        'sendgrid' => [
            'className' => '\JorisVaesen\Sendgrid\Mailer\Transport\SendgridTransport',
            'password' => 'your_sendgrid_api_key',
            'callback' => function (\SendGrid\Mail\Mail $sendgridEmail, \Cake\Mailer\Email $cakephpEmail) {
                if ($cakephpEmail->getSubject() === 'If you open this mail...') {
                    $sendgridEmail->setOpenTracking(true);
                }
                
                return $sendgridEmail;
            },
        ],
        ...
    ],

Usage

$email = new Email('default');
$email->viewBuilder()->setLayout('default);
/* @var \Sendgrid\Response $response */
$response = $email
    ->setProfile('sendgrid')    // optional when you've set sendgrid as the default transport in configuration
    ->setEmailFormat('html')
    ->setSender('[email protected]', 'Example sender')
    ->setFrom('[email protected]', 'Example From')
    ->setReplyTo('[email protected]', 'Example ReplyTo')
    ->setTo('[email protected]', 'Example To')
    ->setSubject('Sample email')
    ->setTemplate('default')
    ->setViewVars([])
    ->send();
    
return $response->statusCode() === 202;

www.datrix.be

About

CakePHP Sendgrid Wrapper

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages