Skip to content

silverbackstudio/yii2-sendinblue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yii2 Framework Sendinblue Mailer and Contacts integration

Provide the following classes:

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

About

Yii2 Framework Sendinblue Mailer and Contacts integration

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages