# 引入 # **[PHPMailer](https://github.com/PHPMailer/PHPMailer)** 郵件
github地址:https://github.com/PHPMailer/PHPMailer
第一步下載:https://github.com//PHPMailer/PHPMailer/archive/master.zip、
第二步把下載文件放到 extend 文件夾下面,建立一個文件夾叫做PHPMailer,全部放在里面
第三步 在自己方法里面引入兩個文件
~~~
require_once("../extend/static/PHPMailer/src/PHPMailer.php");
require_once("../extend/static/PHPMailer/src/SMTP.php");
~~~
# Html 代碼頁面
~~~
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Pragma" content="no-cache">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
<meta name="format-detection" content="telephone=yes"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>手機注冊信息</title>
<!-- Bootstrap core CSS-->
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.js"></script>
<script src="https://cdn.bootcss.com/sweetalert/2.1.2/sweetalert.min.js"></script>
<style type="text/css">
body{
margin: 0;
padding: 0;
}
.modal_content{
padding: 30px;
display: flex;
justify-content: center;
flex-direction: column;
}
.modal_content>div{
margin-bottom: 20px;
}
.modal_content>h5:first-child{
margin:30px 0px;
}
#dialog label{
color: #666;
}
#phone1{
display: block;
width: 100%;
height: 70px;
background: none;
padding-top: 30px;
border: 0;
outline:none;
text-align: center;
margin-top: -30px;
font-size: 16px;
border-bottom: 1px solid rgba(0,0,0,.2);
border-radius: 0;
}
.code1{
display: flex;
flex-direction: row;
justify-content: space-between;
width: 100%;
height: 70px;
background: none;
padding-top: 30px;
margin-top: -30px;
font-size: 16px;
border-bottom: 1px solid rgba(0,0,0,.2);
border-radius: 0;
}
#code1{
width: calc(100% - 90px);
height: 55px;
background: none;
padding-top: 20px;
border: 0;
outline:none;
text-align: center;
margin-top: -20px;
font-size: 16px;
}
#btnSendCode1{
width: 90px;
height: 30px;
padding: 0 5px;
margin: 0;
font-size: 14px;
text-align: center;
background: transparent;
border-radius: 30px;
color: #a07941;
border-color: #a07941;
}
::-webkit-input-placeholder { /* WebKit browsers */
font-size: 14px;
color: rgba(0,0,0,.4);
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
font-size: 14px;
color: rgba(0,0,0,.4);
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
font-size: 14px;
color: rgba(0,0,0,.4);
}
:-ms-input-placeholder { /* Internet Explorer 10+ */
font-size: 14px;
color: rgba(0,0,0,.4);
}
.next{
text-align: center;
margin: 20px 0;
}
.next button{
width: 100%;
height: 45px;
padding: 0;
margin: 0;
background: #007BFF;
color: #fff;
border: 0;
outline:none;
border-radius: 3px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3"></div>
<div class="modal_content col-md-6">
<form action="post" id="register">
<div>
<label for="phone1">手機號:</label><br />
<input id="phone1" type="text" autocomplete="off" name="phone" value="461476246@qq.com" placeholder="請輸入已綁定的手機號"/>
</div>
<div>
<label for="phone1">密碼:</label><br />
<input type="password" autocomplete="off" name="password" style=" display: block;
width: 100%;
height: 70px;
background: none;
padding-top: 30px;
border: 0;
outline: none;
text-align: center;
margin-top: -30px;
font-size: 16px;
border-bottom: 1px solid rgba(0,0,0,.2);
border-radius: 0;"/>
</div>
<div>
<label for="code1">驗證碼:</label>
<div class="code1">
<input id="code1" type="text" name="codes" autocomplete="off" placeholder="短信驗證碼"/>
<button id="btnSendCode1" type="button" class="btn btn-default" value="" >獲取驗證碼</button>
</div>
</div>
<div class="next">
<button onClick="binding()" type="button">確定</button>
</div>
</form>
</div>
<div class="col-md-3"></div>
</div>
</div>
<script language="javascript">
// var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;//手機號正則
let phoneReg = /(^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$)/; //郵箱驗證
$(function () {
//點擊發送驗證碼
$('body').on('click', '#btnSendCode1', function () {
let phone = $.trim($('#phone1').val());
if (!phoneReg.test(phone)) {
alert(" 請輸入有效的手機號碼");
return false;
}
$('body').off('click', '#btnSendCode1');
//向后臺發送處理數據
$.ajax({
type: 'post',
//提交后臺php文件
url: "{:url('index/index/email')}",
// 得到表單提交的數據
data:{'phone':phone}
,
dataType: 'json',
//回調處理
success: function(data){
if (data.status ==1) {
swal({
//彈出后臺返回的消息
text: data.message,
icon: "success",
button: false
});
}else{
swal({
text: data.message,
icon: "warning",
button: false
});
}
}
})
LockButton('#btnSendCode1', 60);
})
//讀取cookie
if ($.cookie("djsendtime") != undefined && !isNaN($.cookie("djsendtime"))) { //讀取到了cookie值
let djsendtime = $.cookie("djsendtime");
let now = new Date().getTime(); //當前時間戳
let locksecends = parseInt((djsendtime - now) / 1000);
if (locksecends <= 0) {
$.cookie("djsendtime", null);
} else {
LockButton('#btnSendCode1', locksecends);
}
}
})
// 按鈕倒計時
var LockButton = function (btnObjId, locksecends) {
//1.獲取當前系統時間
//2.獲取 locksecends 后的系統時間
//3.用cookie保存到期時間
//4.每次加載后獲取cookie中保存的時間
//5.用到期時間減去當前時間獲取倒計時
let djsendtime = $.cookie("djsendtime");
if (djsendtime == null || djsendtime == undefined || djsendtime == 'undefined' || djsendtime == 'null') {
var now = new Date().getTime(); //當前時間戳
var endtime = locksecends * 1000 + now; //結束時間戳
$.cookie("djsendtime", endtime); //將結束時間保存到cookie
}
$(btnObjId).addClass('disabled').attr('disabled', 'disabled').text( + locksecends + '秒后獲取');
$('body').off('click', '#btnSendCode1SMS');
let timer = setInterval(function () {
locksecends--;
$(btnObjId).text(locksecends + '秒后獲取');
if (locksecends <= 0) {
//倒計時結束清除cookie值
$.cookie("djsendtime", null);
$(btnObjId).removeClass('disabled').removeAttr('disabled').text('重新獲取');
clearInterval(timer);
}
}, 1000);
};
/*提交*/
function binding(){
//向后臺發送處理數據
$.ajax({
type: 'post',
//提交后臺php文件
url: "{:url('index/index/res')}",
// 得到表單提交的數據
data:$('#register').serialize()
,
dataType: 'json',
//回調處理
success: function(data){
if (data.status ==1) {
swal({
//彈出后臺返回的消息
text: data.message,
icon: "success",
button: false
});
}else{
swal({
text: data.message,
icon: "warning",
button: false
});
}
}
})
}
</script>
</body>
</html>
~~~

# php 處理代碼
~~~
//發送郵件
public function email(){
$email = \think\facade\Request::post('phone');
require_once("../public/static/PHPMailer/src/PHPMailer.php");
require_once("../public/static/PHPMailer/src/SMTP.php");
// 實例化PHPMailer核心類
$mail = new \PHPMailer\PHPMailer();
// 是否啟用smtp的debug進行調試 開發環境建議開啟 生產環境注釋掉即可 默認關閉debug調試模式
$mail->SMTPDebug = 1;
// 使用smtp鑒權方式發送郵件
$mail->isSMTP();
// smtp需要鑒權 這個必須是true
$mail->SMTPAuth = true;
// 鏈接qq域名郵箱的服務器地址
$mail->Host = 'smtp.qq.com';
// 設置使用ssl加密方式登錄鑒權
$mail->SMTPSecure = 'ssl';
// 設置ssl連接smtp服務器的遠程服務器端口號
$mail->Port = 465;
// 設置發送的郵件的編碼
$mail->CharSet = 'UTF-8';
// 設置發件人昵稱 顯示在收件人郵件的發件人郵箱地址前的發件人姓名
$mail->FromName = 'Mrs gao';
// smtp登錄的賬號 QQ郵箱即可
$mail->Username = '461476246@qq.com';
// smtp登錄的密碼 使用生成的授權碼
$mail->Password = 'rjipjeasbxaybgec';
// 設置發件人郵箱地址 同登錄賬號
$mail->From = '461476246@qq.com';
// 郵件正文是否為html編碼 注意此處是一個方法
$mail->isHTML(true);
// 設置收件人郵箱地址
$mail->addAddress($email);
// 添加該郵件的主題
$mail->Subject = '郵件主題';
// 添加郵件正文
$mail->Body = '<h1>Hello World'.$this->codes().'
</h1>';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//郵件發送,必須寫
$mail -> send() ;
// 發送郵件 返回狀態
return ['status'=>'1','message'=>'郵件發送成功'];
}
//定義一個六位驗證碼
public function codes($len =6){
$str = '';
for($i=0;$i<$len;$i++){
$str .= mt_rand(0,9);
}
session('codes',$str);
session('time',time()+60);
return $str;
}
//接受數據驗證處理
public function res(Request $request){
$data =$request->param();
if ($data['codes'] !=\session('codes')){
return ['status'=>'1','message'=>'兩次驗證碼不一致'];
}
if (time() >=\session('time')){
return ['status'=>'1','message'=>'驗證碼已經過期'];
}
//后面就是插入數據庫處理
$ifno = Db::table('users')->data([
'phone'=>$data['phone'],
'password'=>sha1($data['password'])
])->insert();
if ($ifno){
return ['status'=>'1','message'=>'注冊成功'];
}
}
~~~
- Tp 框架5.1安裝
- Tp 資源控制器創建跟非資源控制器
- Tp 自定義目錄
- Tp 鏈接傳參,地址欄獲取參數
- Tp ajax 異步提交數據
- Tp 模型里面指定開啟時間戳
- Tp加載驗證碼
- Tp 模版
- Tp 模版繼承
- Tp模版頁面If 條件 及session存儲 取出
- Tp 默認在模版頁面指定內容
- Tp 模版頁面獲取session
- Tp驗證器
- Tp 自定義驗證
- Thinkphp 模型驗證規則使用
- Tp 文件上傳
- Tp 實現注冊自動登錄
- Tp 編輯器
- Tp 結合 Markdown里面的editor編輯器
- Tp Marckdown 編輯模版輸出
- Tp 搜索功能
- Tp 點贊 收藏
- Tp 基本權限
- Tp 創建驗證器
- Sweetalert 彈出框 引用
- Tp 網站 開啟 關閉功能
- Tp 網站 開啟 注冊功能
- Tq QQ登錄
- Tp 郵箱注冊 驗證
- Tp 無限極分類
- Tp 路由
- Tp RBAC權限 適合于任何框架
- 創建權限模型,控制器,驗證規則
- 創建角色 控制器 模型 驗證規則
- 用戶角色設置
- 權限驗證
- Tp auth 權限
- 第一部 auth 安裝
- 第二部 創建 三張數據庫
- 第三部 創建用戶控制器
- 第四部 創建規則表
- 規則表基本操作
- 1 規則頁面加載
- 2 添加規則
- 3 編輯規則
- 4 刪除規則
- 第五部 創建用戶組
- 用戶組基本操作
- 第一步 展示用戶組首頁
- 第二步 添加用戶組
- 第三步 編輯用戶組
- 第四部 刪除用戶組
- 第六步設置權限
- 第一步 創建 模型 控制器
- 第二步 首頁顯示
- 第三部 設置用戶權限
- 第七步 執行權限認證
- 備注 附加規則原理
- Tp 數據庫
- 外鍵約束
- Tp 結合layui 上傳
- Layui 編輯器 前端展示
- Tp 模版eq
- Tp 刪除
- Tp session 處理
- Tp cache緩存方法
- Tp 后臺驗證管理員登錄
- 前臺Nav 導航
- 左邊導航
- Tp 批量刪除
- O2O
- Tp框架插入數據自帶時間戳
- Tp 結合layui 分頁
- Tp 整合百度地圖
- Tp 結合城市三級聯動
- 密碼驗證
- 團購時間
- Tp模板時間修改
- 安卓App 平臺入駐上線
- Api 接口返回
- Thinkphp 生成5.1二維碼
- 回調Api
- layui 關閉框 刷新
- Layui 監聽
- 判斷是否是pc端 手機端
- Lauyi 自動轉換時間
- Tp 增加時間
- ajax 同步 異步處理
- 彈出框
- 傳輸數據編輯
- 上傳 引用 uploadify
- js 收縮
- Tp 增加數據表
- Tp-前置鉤子
- 刪除代碼
- 適合tp5.0描述文字過長
- php 三級城市城市表帶數據
- layu結合 tp table數據數組循環
- layui 解決數據為NUll 錯誤代碼