开通阿里云短信服务
登录阿里云进入控制台 产品与服务–>云通讯–>短信服务–>邮件推送,然后开通
集成到TP5框架
安装
参照本人的博客:《短信服务之阿里云平台》
使用
可以在common文件夹增加一个Email类
贴上代码
<?php
namespace app\common;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class Email {
/**
* 发送邮箱验证码
* @param string email 邮箱地址
* @param int number 验证码
* @return array
*/
public function sendAuthorizedCode(string $email, int $number) : array
{
try {
AlibabaCloud::accessKeyClient(env('ACCESS_KEY_ID'), env('ACCESS_KEY_SECRET'))
->regionId('cn-hangzhou')
->asDefaultClient();
$options = [
'query' => [
'RegionId' => "cn-hangzhou",
'AccountName' => 'msc_5201399@qq.com',
'AddressType' => 1,
'ReplyToAddress' => true,
'Subject' => '爱巢科技注册',
'ToAddress' => $email,
'FromAlias' => '爱巢科技',
'TagName' => 'DiDong',
'HtmlBody' => $this->getEmailContentTemplate($number),
]
];
$result = AlibabaCloud::rpc()
->product('Dm')
->version('2015-11-23')
->action('SingleSendMail')
->method('POST')
->host('dm.aliyuncs.com')
->options($options)
->request();
if($result && $result->EnvId) {
$code = 0;
$msg = '发送成功';
//把生成的随机数存进redis缓存
$key = 'zc_code_' . $email;
app('mycache')->set($key, $number, 300);
}else{
$code = 2;
$msg = $result->Message;
}
return [$code, $msg];
} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}
}
/**
* 获取邮件内容模板
* @param int code 验证码
* @return string
*/
private function getEmailContentTemplate(int $code) : string
{
return <<<FOR
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>爱巢科技</title>
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1,minimum-scale=1,user-scalable=no">
<style>
* {
margin: 0;
padding: 0;
}
.container {
background: #fff;
width: calc(100% - 20px);
max-width: 700px;
margin: 0 auto;
padding: 10px;
}
img{
width: 140px;
display: block;
margin: 10px auto 30px;
}
.title-view {
display: flex;
align-items: center;
justify-content: space-between;
height: 50px;
padding: 0 10px;
}
.title-view .title {
font-size: 16px;
color: #333;
font-weight: bold;
}
.title-view .tips {
font-size: 12px;
color: #999999;
}
.content {
border: 1px solid #999;
border-radius: 10px;
overflow: hidden;
}
.code-view {
padding: 20px 10px;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.code-view .code {
font-size: 16px;
line-height: 24px;
}
.code-view span {
font-size: 28px;
color: #3399ff;
}
.code-view .tips {
font-size: 14px;
}
.tips-view{
padding: 5px 10px;
background: #e6e6e6;
}
.tips-view p{
font-size: 12px;
color: #666;
}
.wechat{
font-size: 14px;
color: #666;
margin-top: 6px;
}
</style>
</head>
<body>
<div class="container">
<img src="http://****.png" alt="">
<div class="content">
<div class="title-view">
<div>
<p class="title">亲爱的用户:</p>
<p class="title" style="font-size: 14px;color: #999;">Dear user:</p>
</div>
</div>
<div class="code-view">
<p class="code">您的验证码</p>
<p class="tips">(Your verification code)</p>
<span id="code">{$code}</span>
</div>
<div class="tips-view">
<p>验证码5分钟内有效</p>
<p style="margin: 0 0 5px 0;">(Verification code valid within 5 minutes)</p>
<p>如果你非验证码登录,请忽略该邮件</p>
<p>(If you are not registered, please ignore this email.)</p>
</div>
</div>
<p class="wechat">微信客服:msc_5201399</p>
<p class="wechat">(WeChat customer service)</p>
</div>
</body>
</html>
FOR;
}
}
顺便把邮箱验证码发送和验证一块贴出来吧,上面那一块是真正的调用邮件推送服务,下面是发送服务前和发送服务后的操作
<?php
namespace app\index\controller;
use think\Validate;
use app\common\Email as EmailService;
class EmailMsg {
/**
* 发送邮箱验证码
*/
public function sendLoginCode(EmailService $emailService)
{
try{
$rule = [
'email' => 'require'
];
$errorMsg = [
'email.require' => '邮箱不能为空'
];
$validate = Validate::make($rule, $errorMsg);
$check = $validate->check(request()->post());
if(!$check){
$msg = $validate->getError();
return json(['code' => 40000, 'msg' => $msg]);
}
$email = request()->post('email');//获取邮箱
$number = mt_rand(100000, 999999);//生成6位数字码
//发送
list($code, $msg) = $emailService->sendAuthorizedCode($email, $number);
if($code){
return json(['code' => $code, 'msg' => $msg]);
}
return json(['code' => $code, 'msg' => '发送成功']);
}catch(ClientException $e){
throw new Excetpion($e->getErrorMessage());
}catch(ServerException $e){
throw new Excetpion($e->getErrorMessage());
}catch(Exception $e){
throw $e;
}
}
/**
* 检测验证码有效性
*/
public function checkLoginCode()
{
try{
$rule = [
'email' => 'require',
'code' => 'require|number'
];
$errorMsg = [
'email.require' => '邮箱不能为空',
'code.require' => '验证码不能为空',
'code.number' => '验证码必须为数字'
];
$validate = Validate::make($rule, $errorMsg);
$check = $validate->check(request()->post());
if(!$check){
$msg = $validate->getError();
return json(['code' => 4000, 'msg' => $msg]);
}
$email = request()->post('email');//手机号码
$code = request()->post('code');//验证码
$key = 'zc_code_' . $email;
$validCode = app('mycache')->get($key);
//0为不通过, 1为通过
$data = [
'is_valid' => 0
];
if($code == $validCode){
$data['is_valid'] = 1;
}
return json(['code' => 0, 'msg' => '验证成功', 'data' => $data]);
}catch(Exception $e){
throw $e;
}
}
}