當我們要做郵箱驗證碼的時候,利用其它第三方的郵件smtp,進行郵件發(fā)送會出現(xiàn)用戶收不到郵件,或者收到郵件會很慢,而阿里云郵件推送的功能可以解決,穩(wěn)定實時的發(fā)送郵件給客戶,
首先在阿里云郵件推送設(shè)置一個域名
再到域名哪里解析如下:
解析成功后嚴重通過后就可以寫程序
https://github.com/aliyun/openapi-sdk-php
在這個地址下載php sdk
vendor('sdk.vendor.autoload');
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\NlsCloudMeta\NlsCloudMeta;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
引入sdk文件
vendor('sdk.vendor.autoload');
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\NlsCloudMeta\NlsCloudMeta;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class AliyunemailService {
function __construct() {
// $fwconfig = D('Config')->getAll();
$config = array(
'MAIL_CHARSET' => 'UTF-8',
'MAIL_SMTPAUTH' => true, //是否使用身份驗證
'MAIL_ISHTML' => true, //是否使用html
'MAIL_USERNAME' => '', //發(fā)件地址
// 'MAIL_USERNAME' => 'send3@szfangwei.cn', //發(fā)件地址
'MAIL_SMTPHOST' => '', //smtp地址
'MAIL_PASSWORD' => '', //發(fā)件地址密碼
'MAIL_GET_ADDRESS' => '', //默認收件箱
);
$this->mailconfig = $config;
}
/**
* 發(fā)送郵件
* @param $get_email
* @param $data array('title'=>'','title2'=>'','detail'=>'')
* @return bool
*/
public function sendEmail($data, $get_email = '') {
$mail = $this->setBaseConfig($data, $get_email);
return $mail;
}
/**
* 郵件基本設(shè)置
* @param $mail
* @param $data
* @param string $get_email
* @return mixed
*/
public function setBaseConfig($data, $get_email = '') {
AlibabaCloud::accessKeyClient('LTAImdH845znjHst', 'Vsimrg02TDW1wrM100LvvEbGtrGu6S')
->regionId('cn-hangzhou')
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()
->product('Dm')
// ->scheme('https') // https | http
->version('2015-11-23')
->action('SingleSendMail')
->method('POST')
->host('dm.aliyuncs.com')
->options([
'query' => [
'RegionId' => "cn-hangzhou",
'AccountName' => "",//發(fā)件人
'AddressType' => "1",
'ReplyToAddress' => "false",
'ToAddress' => $get_email,//收件人
'Subject' => $data['title'],
'TagName' => "tag",//不用管
'HtmlBody' => $data['detail'],//發(fā)送html格式的內(nèi)容
'TextBody' => "",//發(fā)送文本內(nèi)容
'FromAlias' => $data['title2'],//發(fā)件人昵稱
'ReplyAddress' => "",//回件郵箱
'ReplyAddressAlias' => "客戶",//不用管
'ClickTrace' => "0",
],
])
->request();
return true;
} catch (ClientException $e) {
return $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
return $e->getErrorMessage() . PHP_EOL;
}
}
}
調(diào)用郵件發(fā)送:
$AliyunemailService = new Service\AliyunemailService();
$result = $AliyunemailService->sendEmail($email_data,$data['email']);
需要注意的是阿里云的郵件推送功能 每天有200封免費的,用戶量大的話,超過的話是 超過就需要購買郵件推送資源包
價格相比短信驗證還是比較便宜的,性價比還是可以的,當然你的服務(wù)器配置很多網(wǎng)速好的話也可提供提供郵件的速度和穩(wěn)定性。
如沒特殊注明,文章均為方維網(wǎng)絡(luò)原創(chuàng),轉(zhuǎn)載請注明來自http://m.oulysa.com/news/5721.html