PHP code example of jlorente / yii2-brevo

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

    

jlorente / yii2-brevo example snippets


...
    'components' => [
        // ... other configurations ...
        'mailer' => [
            'class' => \jlorente\brevo\Mailer::class,
            'apiKey' => env('BREVO_API_KEY'),
            'defaultSender' => [
                'email' => '[email protected]',
                'name'  => 'My App',
            ],
            'sandboxEmail' => '[email protected]', // or an array of addresses
            'guzzleConfig' => [
                'timeout' => 10.0,
                // add proxy or other Guzzle options if needed
            ],
            'useFileTransport' => false,
            'viewPath' => '@app/mail',
        ],
    ],
...

    Yii::$app->mailer->compose()
        ->setTo('[email protected]')
        ->setSubject('Hello from Brevo')
        ->setHtmlBody('<h1>Hello</h1><p>HTML body</p>')
        ->setTextBody('Hello — plain text body')
        ->send();

    Yii::$app->mailer->compose()
        ->setTo('[email protected]')
        ->setTemplateId(123456)
        ->setParams([
            'firstName' => 'John',
            // other template parameters
        ])
        ->send();

    Yii::$app->mailer->compose()
        ->setTo('[email protected]')
        ->setSubject('Report attached')
        ->setHtmlBody('<p>Please find the report attached</p>')
        ->attach('/path/report.pdf', [
            'fileName' => 'report.pdf',
            'contentType' => 'application/pdf',
        ])
        ->send();

    $binary = file_get_contents('/path/generated.pdf');
    Yii::$app->mailer->compose()
        ->setTo('[email protected]')
        ->setSubject('In-memory attachment')
        ->setHtmlBody('<p>See attached file</p>')
        ->attachContent($binary, [
            'fileName' => 'generated.pdf',
            'contentType' => 'application/pdf',
        ])
        ->send();
bash
$ php composer.phar