PHP code example of djagya / yii2-sparkpost

1. Go to this page and download the library: Download djagya/yii2-sparkpost 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/ */

    

djagya / yii2-sparkpost example snippets

 
return [
    //....
    'components' => [
        'mailer' => [
            'class' => 'djagya\sparkpost\Mailer',
            'apiKey' => 'YOUR_API_KEY',
            'viewPath' => '@common/mail',
            'defaultEmail' => '[email protected]', // optional if 'adminEmail' app param is specified or 'useDefaultEmail' is false
            'retryLimit' => 5, // optional
        ],
    ],
];
 
return [
    //....
    'components' => [
        'mailer' => [
            'class' => 'djagya\sparkpost\Mailer',
            'apiKey' => 'YOUR_API_KEY',
            'viewPath' => '@common/mail',
            'httpAdapter' => 'Ivory\HttpAdapter\Guzzle6HttpAdapter', // OR array or closure
        ],
    ],
];

Yii::$app->mailer->compose('contact/html')
    ->setFrom('[email protected]')
    ->setTo($to)
    ->setSubject($from)
    ->send();

$mailer = Yii::$app->mailer;

$mailer->lastTransmissionId; // string, id of the last transmission
$mailer->lastError; // APIResponseException we got from Sparkpost library with detailed information from the response
$mailer->sentCount; // int, amount of successfuly sent messages
$mailer->rejectedCount; // int, amount of rejected messages
 
return [
    //....
    'components' => [
        'mailer' => [
            'class' => 'djagya\sparkpost\Mailer',
            'apiKey' => 'YOUR_API_KEY',
            'sandbox' => true,
            'defaultEmail' => 'test@SPARKPOST_SANDBOX_DOMAIN',
        ],
    ],
];
 
Yii::$app->mailer->compose('contact/html')
    ->setSandbox(true)
    ->setFrom('test@SPARKPOST_SANDBOX_DOMAIN')
    ->setTo($to)
    ->setSubject($subject)
    ->send();
 
Yii::$app->mailer->compose(['template' => 'sparkpost_template_id'], ['template_param' => 'value1', ...])
    ->setTo($to)
    ->send();

Yii::$app->mailer->compose()
    ->setTemplateId('sparkpost_template_id')
    ->setSubstitutionData(['template_param' => 'value1', ...])
    ->setTo($to)
    ->send();

$to = [
    '[email protected]' => [
        'name' => 'Recipient #1',
        'metadata' => [
            'key' => 'value',
        ],
        'substitution_data' => [
            'template_key' => 'value',
        ],
        'tags' => ['tag1', 'tag2'],
    ],
    // ... other possible addresses
];

Yii::$app->mailer->compose(['template' => 'sparkpost_template_id'], ['template_param' => 'value1', ...])
    ->setTo($to)
    ->send();

php composer.phar 

php /vendor/bin/codecept run

APIKEY=your_api_key php /vendor/bin/codecept run