silverback / yii2-sendinblue
Sendinblue 集成和用于 Yii 框架的邮件发送器
1.0.1
2019-10-08 14:25 UTC
Requires
- sendinblue/api-v3-sdk: ^6.0
- yiisoft/yii2: ^2.0.14
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-09 00:26:19 UTC
README
提供以下类
- yii/sendinblue/transactional/Mailer: 实现了 MailerInterface 的类,并使用 Sendinblue API v3 发送邮件
- yii/sendinblue/transactional/Message: 实现了 MessageInterface 的类,用于标准邮件
两个消息类型的选择将由 compose()
方法自动完成。
安装
使用 Composer 在 Yii 项目根目录中安装此包。
composer require silverback/yii2-sendinblue
设置
在应用程序配置中设置 Sendinblue 邮件发送器
'components' => [ //... 'mailer' => [ 'class' => 'yii\sendinblue\transactional\Mailer', 'apikey' => 'your-sedinblue-api-key', ], //... ]
用法
使用视图发送邮件(标准的 Yii 行为)
$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"; }
发送带有自定义文本的邮件
$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"; }
使用 Sendinblue 模板发送邮件
$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"; }
以下属性将作为替换项在模板中使用
%ATTR1%
%ATTR2__SUBATTR1%
%ATTR2__SUBATTR2__SUBSUBATTR1%
所有属性都将被转换,并必须使用大写字母。
测试
此类使用 PHPUnit 作为测试套件,要测试类和函数,请按照以下步骤操作。
将库文件夹中的 phpunit.xml.dist
文件复制到 phpunit.xml
,并在其中定义 Api-Key 和地址
<php> <const name="SENDINBLUE_API_KEY" value="{your-key}"/> <const name="SENDINBLUE_TEMPLATE" value="1"/> <const name="SENDINBLUE_FROM" value="from@example.com"/> ... </php>
运行 composer update
安装所有依赖项和测试套件。
使用以下命令运行测试
./vendor/bin/phpunit tests/ # all tests ./vendor/bin/phpunit tests/TemplateMessageTest # single test