2017 © Pedro Peláez
 

cakephp-plugin cakephp-sendgrid

Sendgrid wrapper for CakePHP

image

jorisvaesen/cakephp-sendgrid

Sendgrid wrapper for CakePHP

  • Friday, June 15, 2018
  • by jorisvaesen
  • Repository
  • 1 Watchers
  • 0 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Sendgrid wrapper for CakePHP

Installation

You can install this plugin into your CakePHP application using composer., (*1)

The recommended way to install composer packages is:, (*2)

composer require jorisvaesen/cakephp-sendgrid

Load the plugin in config/bootstrap.php:, (*3)

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

Configuration

Method 1 (using ENV)

The plugin loads from dotenv the following:, (*4)

SENDGRID_API_KEY="your_sendgrid_api_key"

Method 2 (using app.php)

Set the sendgrid api key in app.php:, (*5)

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

Method 3 (hardcoded)

Add in your app.php file, in the EmailTransport item, (*6)

'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:, (*7)

'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:, (*8)

'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('noreply@example.com', 'Example sender')
    ->setFrom('from@example.com', 'Example From')
    ->setReplyTo('replyTo@example.com', 'Example ReplyTo')
    ->setTo('to@example.com', 'Example To')
    ->setSubject('Sample email')
    ->setTemplate('default')
    ->setViewVars([])
    ->send();

return $response->statusCode() === 202;

www.datrix.be, (*9)

The Versions

15/06 2018

dev-master

9999999-dev

Sendgrid wrapper for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

cakephp sendgrid

15/06 2018

0.2.0

0.2.0.0

Sendgrid wrapper for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

cakephp sendgrid

15/06 2018

0.1.1

0.1.1.0

Sendgrid wrapper for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

cakephp sendgrid