silverback / yii2-sendinblue
Sendinblue integration and Mailer for the Yii framework
Installs: 1 859
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 3
Open Issues: 1
Type:yii2-extension
Requires
- sendinblue/api-v3-sdk: ^6.0
- yiisoft/yii2: ^2.0.14
Requires (Dev)
This package is auto-updated.
Last update: 2024-12-09 01:03:23 UTC
README
Provide the following classes:
- yii/sendinblue/transactional/Mailer: A class that implements MailerInterface and uses Sendinblue API v3 to send email
- yii/sendinblue/transactional/Message: A class that implements MessageInterface for standard emails
The choice between the two message types is made automatically by the compose()
method.
Installation
Install this package in Yii project root with Composer.
composer require silverback/yii2-sendinblue
Setup
Setup the Sendinblue mailer in app config:
'components' => [ //... 'mailer' => [ 'class' => 'yii\sendinblue\transactional\Mailer', 'apikey' => 'your-sedinblue-api-key', ], //... ]
Usage
Send email using a view (standard Yii behavior)
$viewAttributes = array( 'attribute1' => 'value1', ); $message = \Yii::$app->mailer->compose('view-name', $viewAttributes); $message->setFrom( 'noreply@example.com' ); $message->setSubject( 'Subject' ); $message->setTo( 'user@example.com' ); if ( $message->send() ) { echo "Sent successfully"; }
Send email with custom text
$message = \Yii::$app->mailer->compose(); $message->setFrom( 'noreply@example.com' ); $message->setSubject( 'Subject' ); $message->setTo( 'user@example.com' ); $message->setTextBody( 'test content' ); if ( $message->send() ) { echo "Sent successfully"; }
Send email with Sendinblue template
$template_id = 1; $templateAttributes = array( 'attr1' => 'value1', 'attr2' => array( 'subattr1' => 'value2', 'subattr2' => array( 'subsubattr1' => 'value2', ) ), ); // The class uses Sendiblue templates when the view name is an integer instead of string. $message = \Yii::$app->mailer->compose( $template_id, $templateAttributes ); $message->setTo( 'user@example.com' ); if ( $message->send() ) { echo "Sent successfully"; }
The following attributes will be available as replacements in the template:
%ATTR1%
%ATTR2__SUBATTR1%
%ATTR2__SUBATTR2__SUBSUBATTR1%
All attributes will be converted and must be used in uppercase.
Testing
This class uses PHPUnit as test suite, to test the classes and functions follow this steps.
Copy the file phpunit.xml.dist
in phpunit.xml
in the library folder and define Api-Key and addresses inside it:
<php> <const name="SENDINBLUE_API_KEY" value="{your-key}"/> <const name="SENDINBLUE_TEMPLATE" value="1"/> <const name="SENDINBLUE_FROM" value="from@example.com"/> ... </php>
Launch a composer update
to install all the dependencies and test suite.
Run the test with the following commands
./vendor/bin/phpunit tests/ # all tests ./vendor/bin/phpunit tests/TemplateMessageTest # single test