当我们要做邮箱验证码的时候,利用其它第三方的邮件smtp,进行邮件发送会出现用户收不到邮件,或者收到邮件会很慢,而阿里云邮件推送的功能可以解决,稳定实时的发送邮件给客户,
首先在阿里云邮件推送设置一个域名
再到域名哪里解析如下:
解析成功后严重通过后就可以写程序
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' => '', //发件地址
// 'MAIL_USERNAME' => 'send3@szfangwei.cn', //发件地址
'MAIL_SMTPHOST' => '', //smtp地址
'MAIL_PASSWORD' => '', //发件地址密码
'MAIL_GET_ADDRESS' => '', //默认收件箱
);
$this->mailconfig = $config;
}
/**
* 发送邮件
* @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;
}
/**
* 邮件基本设置
* @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' => "",//发件人
'AddressType' => "1",
'ReplyToAddress' => "false",
'ToAddress' => $get_email,//收件人
'Subject' => $data['title'],
'TagName' => "tag",//不用管
'HtmlBody' => $data['detail'],//发送html格式的内容
'TextBody' => "",//发送文本内容
'FromAlias' => $data['title2'],//发件人昵称
'ReplyAddress' => "",//回件邮箱
'ReplyAddressAlias' => "客户",//不用管
'ClickTrace' => "0",
],
])
->request();
return true;
} catch (ClientException $e) {
return $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
return $e->getErrorMessage() . PHP_EOL;
}
}
}
调用邮件发送:
$AliyunemailService = new Service\AliyunemailService();
$result = $AliyunemailService->sendEmail($email_data,$data['email']);
需要注意的是阿里云的邮件推送功能 每天有200封免费的,用户量大的话,超过的话是 超过就需要购买邮件推送资源包
价格相比短信验证还是比较便宜的,性价比还是可以的,当然你的服务器配置很多网速好的话也可提供提供邮件的速度和稳定性。