chocoboxxf / yii2-sendcloud-sdk
SendCloud API for Yii 2.0
Installs: 76
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ~6.0
- yiisoft/yii2: *
Requires (Dev)
- phpunit/phpunit: >=4.0
This package is not auto-updated.
Last update: 2024-11-09 20:50:44 UTC
README
基于Yii2实现的SendCloud API SDK(目前开发中)
环境条件
-
= PHP 5.4
-
= Yii 2.0
-
= GuzzleHttp 6.0
安装
添加下列代码在composer.json
文件中并执行composer update --no-dev
操作
{ "require": { "chocoboxxf/yii2-sendcloud-sdk": "dev-master" } }
设置方法
// 全局使用 // 在config/main.php配置文件中定义component配置信息 'components' => [ ..... 'mail' => [ 'class' => 'chocoboxxf\SendCloud\SendCloud', 'apiUser' => 'API_USER', // API User 'apiKey' => 'API_KEY', // API Key 'defaultFrom' => 'default@default.com', // 缺省发件人地址 'defaultFromName' => 'default', // 缺省发件人名称 ] .... ] // 代码中调用 $result = Yii::$app->mail->sendTemplateMail( 'TEMPLATE_NAME', ['user_a@company.com', 'user_b@company.com'], 'admin@company.com', 'admin', 'Welcome Letter', ['key1' => 'value1 for all', 'key2' => ['value2 for a', 'value2 for b']], ['/path/to/file1', '/path/to/file2'] ); ....
// 局部调用 $mailService = Yii::createObject([ 'class' => 'chocoboxxf\SendCloud\SendCloud', 'apiUser' => 'API_USER', // API User 'apiKey' => 'API_KEY', // API Key 'defaultFrom' => 'default@default.com', // 缺省发件人地址 'defaultFromName' => 'default', // 缺省发件人名称 ]); $result = $mailService->sendTemplateMail( 'TEMPLATE_NAME', ['user_a@company.com', 'user_b@company.com'], 'admin@company.com', 'admin', 'Welcome Letter', ['key1' => 'value1 for all', 'key2' => ['value2 for a', 'value2 for b']], ['/path/to/file1', '/path/to/file2'] ); ....
使用示例
普通发送接口
$subject = '测试邮件'; $content = '<html><h1>this is a test %key% html text</h1></html>'; $to = ['user_a@company.com', 'user_b@company.com']; $from = 'admin@company.com'; $fromName = '管理员'; $templateData = [ 'key' => 'value', ]; $attachments = [ '/path/to/file1', ]; $result = Yii::$app->mail->sendNormalMail($subject, $content, $to, $from, $fromName, $templateData, $attachments); if ($result['result'] === true) { // 正常情况 // 返回数据格式 // { // "statusCode": 200, // "info": { // "emailIdList": [ // "1447054895514_15555555_32350_1350.sc-10_10_126_221-inbound0$user_a@company.com", // "1447054895514_15555555_32350_1350.sc-10_10_126_221-inbound1$user_b@company.com" // ] // }, // "message": "请求成功", // "result": true // } .... } else { // 出错情况 // 返回数据格式 // { // "statusCode": 40863, // "info": {}, // "message": "to中有不存在的地址列表. 参数to: user_a@company.com", // "result": false // } .... } ....
模板发送接口
$subject = '测试邮件'; $template = 'TEMPLATE_NAME'; $to = ['user_a@company.com', 'user_b@company.com']; $from = 'admin@company.com'; $fromName = '管理员'; $templateData = [ 'key1' => 'value1 for a and b', 'key2' => ['value2 for a', 'value2 for b'] ]; $attachments = [ '/path/to/file1', ]; $result = Yii::$app->mail->sendTemplateMail($template, $to, $from, $fromName, $subject, $templateData, $attachments); if ($result['result'] === true) { // 正常情况 // 返回数据格式 // { // "statusCode": 200, // "info": { // "emailIdList": [ // "1447054895514_15555555_32350_1350.sc-10_10_126_221-inbound0$user_a@company.com", // "1447054895514_15555555_32350_1350.sc-10_10_126_221-inbound1$user_b@company.com" // ] // }, // "message": "请求成功", // "result": true // } .... } else { // 出错情况 // 返回数据格式 // { // "statusCode": 40863, // "info": {}, // "message": "to中有不存在的地址列表. 参数to: user_a@company.com", // "result": false // } .... } ....