nickcv/yii2-mandrill
Composer 安装命令:
composer require nickcv/yii2-mandrill
包简介
Mandrill Api Integration for Yii2
README 文档
README
Mandrill Api Integration for Yii2
Change Log
Since version 1.6.1 the methods nickcv\mandrill\Message::setHtmlBody and nickcv\mandrill\Message::setSubject do not purify/encode the data automatically.
This has been done to offer a greater degree of flexibility to developers, see issues #16 and #19.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist nickcv/yii2-mandrill "*"
or add
"nickcv/yii2-mandrill": "*"
to the require section of your composer.json file.
Set Up
Recently Mandrill became a MailChilmp service: to use Mandrill you will need to have a MailChimp Account.
For details on the pricing please check their website.
Once you have an account you will need to create an API Key. You can create as many API keys as you want, and it's best practice to create one for each website. You can also create test API keys. Every email submitted using a test API key will not actually be submitted, but you'll be able to check inside the test dashboard if the test went thorugh successfully.
Mandrill will keep track of every single email you submit. You can filter the data using tags and you'll also be able to check how many times each email was opened and if the links within it have been clicked.
Usage
Once the extension is installed, change your application config file web.php:
First of all you will need to add an application name.
By default this extension will send every single email using the application name as the sender name and the adminEmail parameter inside params.php as the sender email.
[ 'id' => 'basic', 'name' => 'Application Name', ]
return [ 'adminEmail' => 'admin@example.com', ];
You will then need to add the component
'mailer' => [ 'class' => 'nickcv\mandrill\Mailer', 'apikey' => 'YourApiKey', ],
From now on you can just use the mandrill mailer just as you used to use the default one.
\Yii::$app->mailer ->compose('mailViewName', ['model' => $model]) ->setTo('email@email.com') ->send();
Mandrill Templates
You can use Mandrill's own template system if you want to, just set up as true the useMandrillTemplates attribute in the component configuration
'mailer' => [
'class' => 'nickcv\mandrill\Mailer',
'apikey' => 'YourApiKey',
'useMandrillTemplates' => true,
],
If you do turn this feature on the component will look for a template within mandrill named after the view argument of the compose method.
Since version 1.3.0 the component will stop falling back to rendering the internal views. This change has been made because now the mandrill send-template method will be used, avoiding to make two API calls when templates are enabled.
Since version 1.4.0 the component won't default to the application name and admin email when using mandrill templates. This has been done to allow the use of Mandrill defaults values for the template.
To override this behavior you can set to false the useTemplateDefaults attribute in the component configuration
'mailer' => [ 'class' => 'nickcv\mandrill\Mailer', 'apikey' => 'YourApiKey', 'useMandrillTemplates' => true, 'useTemplateDefaults' => false, ],
Since version 1.5.0 the component has a configurable property templateLanguage that can contain either 'mailchimp' or 'handlebars' ('mailchimp' is by default).
For more information about handlebars usage check these links:
You can change preferred language by editing templateLanguage attribute in the component configuration
'mailer' => [ 'class' => 'nickcv\mandrill\Mailer', 'apikey' => 'YourApiKey', 'useMandrillTemplates' => true, 'templateLanguage' => nickcv\mandrill\Mailer::LANGUAGE_HANDLEBARS, ],
Additional Methods
Mandrill lets you set up tags. The method \nickcv\mandrill\Message::setTags($tags) accept as an argument both a string or an array of strings:
\Yii::$app->mailer ->compose('mailViewName', ['model' => $model]) ->setTags(['registration']);
Since version 1.3.0 it's also possible to enable the async mode. When using async mode mandrill will queue the messages and send em in batches. If you send a message to more than 10 email addresses async mode will be used automatically.
\Yii::$app->mailer ->compose('mailViewName', ['model' => $model]) ->enableAsync();
Since version 1.4.0 it's also possible to use global merge vars. This variables will be use to replace placeholders by mandrill. This is especially useful when dealing with templates.
Since version 1.6.0 you can get the Mandrill object used by the component calling the Mailer::getMandrill method.
For more informations check the component documentation.
Since version 1.7.0 it's also possible to enable/disable messages clicks/opens tracking and to decide whether or not it is a priority message.
It is also possible to retrieve the full body of the response returned from Mandrill using the Mailer::getLastTransaction method.
Sending Multiple Email with Mandril Template using merge_vars
You can consider using this method for using mandrill template and change the variable based on your data :
\Yii::$app->mailer ->compose($stringTemplate) ->setTo($arrayTo) ->setGlobalMergeVars($arrayGolbalMergeVars) ->setMergeVars($arrayMergeVars) ->send();
Unit Testing
All the Classes within the package have been unit tested. The tests are included within the package.
If you wish to run the tests install phpunit following the Yii2 documentation.
The tests use the developer Mandrill Test API key which is only whitelisted for the developer IP.
Logs
The component automatically logs every single message sent through mandrill, inside the "mandrill" category.
Messages sent successfully are logged using \Yii::info(), messages rejected or invalid are logged using \Yii::warning(), and all the exceptions thrown by the Mandrill Class are logged using \Yii::error().
If you are using mandrill templates and the template is not found the error will be logged using Yii::info().
nickcv/yii2-mandrill 适用场景与选型建议
nickcv/yii2-mandrill 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 574k 次下载、GitHub Stars 达 29, 最近一次更新时间为 2014 年 08 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mailer」 「email」 「extension」 「mandrill」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nickcv/yii2-mandrill 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nickcv/yii2-mandrill 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nickcv/yii2-mandrill 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
Adds more BBCode
Creates new sare mail transport for laravel applications
统计信息
- 总下载量: 574k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 29
- 点击次数: 31
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0
- 更新时间: 2014-08-14