PHP code example of sprintcube / cakephp-sendgrid

1. Go to this page and download the library: Download sprintcube/cakephp-sendgrid 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/ */

    

sprintcube / cakephp-sendgrid example snippets


Plugin::load('SendGrid');

'EmailTransport' => [
...
  'sendgrid' => [
      'className' => 'SendGrid.SendGrid',
      'apiKey' => 'your-api-key' // your api key
  ]
]

'Email' => [
    'default' => [
        'transport' => 'default',
        'from' => 'you@localhost',
        //'charset' => 'utf-8',
        //'headerCharset' => 'utf-8',
    ],
    'sendgrid' => [
        'transport' => 'sendgrid'
    ]
]

$email = new SendGridMailer();
$email->setFrom(['[email protected]' => 'CakePHP SendGrid'])
    ->setTo('[email protected]')
    ->addTo('[email protected]')
    ->addCc('[email protected]')
    ->setSubject('Email from CakePHP SendGrid plugin')
    ->deliver('Message from CakePHP SendGrid plugin');

$email = new SendGridMailer();
$email->setFrom(['[email protected]' => 'CakePHP SendGrid'])
    ->setTo('[email protected]')
    ->setHeaders([
        'X-Custom' => 'headervalue',
        'X-MyHeader' => 'myvalue'
    ])
    ->setSubject('Email from CakePHP SendGrid plugin')
    ->deliver('Message from CakePHP SendGrid plugin');

$email = new SendGridMailer();
$email->setFrom(['[email protected]' => 'CakePHP SendGrid'])
    ->setTo('[email protected]')
    ->setSubject('Email from CakePHP SendGrid plugin')
    ->setAttachments([
        'cake_icon1.png' => Configure::read('App.imageBaseUrl') . 'cake.icon.png',
        'cake_icon2.png' => ['file' => Configure::read('App.imageBaseUrl') . 'cake.icon.png'],
        WWW_ROOT . 'favicon.ico'
    ])
    ->deliver('Message from CakePHP SendGrid plugin');

$email = new SendGridMailer();
$email->setTo('[email protected]')
    ->setTemplate('d-xxxxxx')
    ->deliver();

$email = new SendGridMailer();
$email->setTo('[email protected]')
    ->setSendAt(1649500630)
    ->deliver();


    'sendgridWebhook' => [
        'tableClass' => 'EmailQueue', // The table name that stores email data
        'uniqueIdField' => 'status_id', // The field name that stores the unique message ID VARCHAR(100)
        'statusField' => 'status', // The field name that stores the status of the email status VARCHAR(100)
        'statusMessageField' => 'status_message', // The field name that stores the status messages TEXT
        'debug' => 'true', // write incoming requests to debug log
        'secure' => 'true', // enable SendGrid signed webhook security. You should enable this in production
        'verification_key' => '<YOUR VERIFICATION KEY>', // The verification key from SendGrid
    ],


    $csrf = new CsrfProtectionMiddleware();

    $csrf->skipCheckCallback(function ($request) {
           // Skip token check for API URLs.
          if ($request->getParam('controller') === 'Webhooks') {
             return true;
            }
    });
 
      // Ensure routing middleware is added to the queue before CSRF protection middleware.
    $middlewareQueue->add($csrf);
 
    return $middlewareQueue;