PHP code example of sprintcube / cakephp-elastic-email

1. Go to this page and download the library: Download sprintcube/cakephp-elastic-email 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-elastic-email example snippets


Plugin::load('ElasticEmail');

'EmailTransport' => [
...
  'elasticemail' => [
      'className' => 'ElasticEmail.ElasticEmail',
      'apiKey' => 'your-api-key' // your api key
  ]
]

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

$email = new Email('elasticemail');
        
$email->setFrom(['[email protected]' => 'CakePHP Elastic Email'])
    ->setSender('[email protected]', 'Someone')
    ->setTo('[email protected]')
    ->addTo('[email protected]')
    ->setHeaders(['X-Custom' => 'headervalue'])
    ->setSubject('Email from CakePHP Elastic Email plugin')
    ->send('Message from CakePHP Elastic Email plugin');

$email = new Email('elasticemail');
$emailInstance = $email->getTransport();
$emailInstance->isTransactional(true);
$email->send();

$email = new Email('elasticemail');
        
$email->setFrom(['[email protected]' => 'CakePHP Elastic Email'])
    ->setSender('[email protected]', 'Someone')
    ->setTo('[email protected]')
    ->addTo('[email protected]')
    ->setHeaders([
        'X-Custom' => 'headervalue',
        'X-MyHeader' => 'myvalue'
    ])
    ->setSubject('Email from CakePHP Elastic Email plugin')
    ->send('Message from CakePHP Elastic Email plugin');

$email = new Email('elasticemail');
        
$email->setFrom(['[email protected]' => 'CakePHP Elastic Email'])
    ->setSender('[email protected]', 'Someone')
    ->setTo('[email protected]')
    ->addTo('[email protected]')
    ->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'
    ])
    ->setSubject('Email from CakePHP Elastic Email plugin')
    ->send('Message from CakePHP Elastic Email plugin');

$email = new Email('elasticemail');
$emailInstance = $email->getTransport();
$emailInstance->setTemplte(123);
$email->send();

$mergeVars = [
    'firstname' => 'Foo',
    'lastname' => 'Bar',
    'title' => 'Good Title'
];

$email = new Email('elasticemail');
$emailInstance = $email->getTransport();
$emailInstance->setMergeVariables($mergeVars);

$email->setFrom(['[email protected]' => 'CakePHP Elastic Email'])
    ->setTo('[email protected]')
    ->setEmailFormat('both')
    ->setSubject('{title} - Email from CakePHP Elastic Email plugin')
    ->send('Hello {firstname} {lastname}, <br> This is an email from CakePHP Elastic Email plugin.');

$email = new Email('elasticemail');
$emailInstance = $email->getTransport();
$emailInstance->setScheduleTime(60); // after 1 hour from sending time
$email->send();