當(dāng)我們要做郵箱驗(yàn)證碼的時(shí)候,利用其它第三方的郵件smtp,進(jìn)行郵件發(fā)送會(huì)出現(xiàn)用戶收不到郵件,或者收到郵件會(huì)很慢,而阿里云郵件推送的功能可以解決,穩(wěn)定實(shí)時(shí)的發(fā)送郵件給客戶,
首先在阿里云郵件推送設(shè)置一個(gè)域名
再到域名哪里解析如下:
解析成功后嚴(yán)重通過(guò)后就可以寫(xiě)程序
https://github.com/aliyun/openapi-sdk-php
在這個(gè)地址下載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, //是否使用身份驗(yàn)證
'MAIL_ISHTML' => true, //是否使用html
'MAIL_USERNAME' => '', //發(fā)件地址
// 'MAIL_USERNAME' => 'send3@szfangwei.cn', //發(fā)件地址
'MAIL_SMTPHOST' => '', //smtp地址
'MAIL_PASSWORD' => '', //發(fā)件地址密碼
'MAIL_GET_ADDRESS' => '', //默認(rèn)收件箱
);
$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ā)件人昵稱(chēng)
'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封免費(fèi)的,用戶量大的話,超過(guò)的話是 超過(guò)就需要購(gòu)買(mǎi)郵件推送資源包
價(jià)格相比短信驗(yàn)證還是比較便宜的,性?xún)r(jià)比還是可以的,當(dāng)然你的服務(wù)器配置很多網(wǎng)速好的話也可提供提供郵件的速度和穩(wěn)定性。
如沒(méi)特殊注明,文章均為方維網(wǎng)絡(luò)原創(chuàng),轉(zhuǎn)載請(qǐng)注明來(lái)自http://www.sdlwjx666.com/news/5721.html