cdcchen/yii2-aliyun

aliyun development sdk

2.0.3 2016-06-26 18:13 UTC

README

composer 安装

composer require cdcchen/yii2-aliyun:^2.0

邮件组件使用教程

配置Yii2组件

'mailer' => [
    'class' => 'cdcchen\yii\aliyun\dm\Mailer',
    'viewPath' => '@common/mail',
    'accessKeyId' => '2ALUK5fzobZI2n5U',
    'accessSecret' => 'OIcUZWFwDuKTmTGp91mF9CqXE9KArk',
],

发送邮件

/* @var \cdcchen\yii\aliyun\dm\Mailer $mailer */
$mailer = \Yii::$app->get('mailer');
/* @var \cdcchen\yii\aliyun\dm\Message $message */
$message = $mailer->setAddressType(SingleSendMailRequest::ADDRESS_TYPE_RAND)
                  ->compose('test');
$result = $message->setTo(['80xxxx97@qq.com', 'cdxxxn@163.com'])
                  ->setFrom('no-reply@dm.163.cn')
                  ->setFromAlias('狗剩他爹')
                  ->setSubject('yii2-aliyun dm mailer test title')
                  ->setTextBody('yii2-aliyun dm mailer test content')
                  ->send();

var_dump($result);

直接使用SingleSendRequest发送邮件

/* @var \cdcchen\yii\aliyun\dm\Mailer $mailer */
$mailer = \Yii::$app->get('mailer');
$client = $mailer->getDmClient();
$request = new SingleSendMailRequest();
$request->setAccountName('no-reply@dm.163.cn')
        ->setFromAlias('狗剩他娘')
        ->setToAddress(['8xxx97@qq.com', 'cxxxn@163.com'])
        ->setSubject('yii2-aliyun dm mailer test title')
        ->setTextBody('yii2-aliyun dm mailer test title');

try {
    $result = $client->execute($request);
    var_dump($result->getData());
} catch (ClientException $e) {
    echo $e->getErrorMessage();
    echo $e->getErrorCode();
    echo $e->getRequestId();
    echo $e->getHostId();
}
finally {
    echo 'Send mail complete.';
}

批量发送邮件

use cdcchen\aliyun\core\base\ClientException;

$request = new \cdcchen\aliyun\dm\BatchSendMailRequest();
$request->setAccountName('info@dm.163.cn')
        ->setTemplateName('【测试】公司全员通知')
        ->setReceiversName('公司员工');

try {
    $response = $client->execute($request);
    var_dump($response->getData());
} catch (ClientException $e) {
    echo $e->getErrorMessage();
    echo $e->getErrorCode();
    echo $e->getRequestId();
    echo $e->getHostId();
}
finally {
    echo 'Send mail complete.';
}

OssClient使用教程

配置Yii2组件

'ossClient' => [
    'class' => 'cdcchen\yii\aliyun\OssClient',
    'accessKeyId' => '2ALUK5fzobZI2n5U',
    'accessSecret' => 'OIcUZWFwDuKTmTGp91mF9CqXE9KArk',
    'endpoint' => 'oss-cn-hangzhou.aliyuncs.com',
    'isCName' => false,
    'defaultBucket' => 'xxxx',
],

获取阿里云官方OssClient实例

/* @var cdcchen\yii\aliyun\OssClient $client */
$client = Yii::$app->get('ossClient');

/* @var \OSS\OssClient $ossClient */
$ossClient = $client->getOssClient();