PHP code example of chocoboxxf / yii2-sendcloud-sdk

1. Go to this page and download the library: Download chocoboxxf/yii2-sendcloud-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chocoboxxf / yii2-sendcloud-sdk example snippets


// 全局使用
// 在config/main.php配置文件中定义component配置信息
'components' => [
  .....
  'mail' => [
    'class' => 'chocoboxxf\SendCloud\SendCloud',
    'apiUser' => 'API_USER', // API User
    'apiKey' => 'API_KEY', // API Key
    'defaultFrom' => '[email protected]', // 缺省发件人地址
    'defaultFromName' => 'default', // 缺省发件人名称
  ]
  ....
]
// 代码中调用
$result = Yii::$app->mail->sendTemplateMail(
    'TEMPLATE_NAME',
    ['[email protected]', '[email protected]'],
    '[email protected]',
    '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' => '[email protected]', // 缺省发件人地址
    'defaultFromName' => 'default', // 缺省发件人名称
]);
$result = $mailService->sendTemplateMail(
    'TEMPLATE_NAME',
    ['[email protected]', '[email protected]'],
    '[email protected]',
    '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 = ['[email protected]', '[email protected]'];
$from = '[email protected]';
$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: [email protected]",
    //   "result": false
    // }
    ....
}
....

$subject = '测试邮件';
$template = 'TEMPLATE_NAME';
$to = ['[email protected]', '[email protected]'];
$from = '[email protected]';
$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: [email protected]",
    //   "result": false
    // }
    ....
}
....