**前期准备**
1.在项目的根目录下通过composer来安装phpmail发送邮件的扩展包
```
composer require phpmailer/phpmailer
```
2.由于前后台都需要发送邮件,因此需要定一个公共函数
app\common.php
```
//////使用的是163邮箱
<?php
/**
* 公共函数
*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function mailto($user,$subject,$content){
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->CharSet='utf-8';
$mail->Host = 'smtp.163.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example@163.com'; // SMTP username
$mail->Password = 'password'; // SMTP 授权码(不是登录密码)
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('example@163.com', '名称');
$mail->addAddress($user); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $content;
return $mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
```
## 1.路由
routes/web.php
```php
Route::namespace('Admin')->group(function () {
Route::match(['GET', 'POST'], '/forget', 'Index@forget');
});
```
## 2.忘记密码页面
admin/index/forget.balde.php
![](https://img.kancloud.cn/5e/b9/5eb90b8043795945b8c7a989b2f7986c_323x263.png)
```html
~~~
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>博客后台管理系统</title>
<link rel="shortcut icon" href="assets/admin/img/logo.jpg" type="image/x-icon">
<link href="assets/admin/css/bootstrap.min.css" rel="stylesheet" />
<link href="assets/admin/css/font-awesome.min.css" rel="stylesheet" />
<link href="assets/admin/css/weather-icons.min.css" rel="stylesheet" />
<link id="beyond-link" href="assets/admin/css/beyond.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="login-container">
<form action="">
<div class="loginbox bg-white">
<div class="loginbox-title">找回密码</div>
<div class="loginbox-or">
<div class="or-line"></div>
</div>
<div class="loginbox-textbox">
<input type="text" class="form-control" name="email" placeholder="请输入邮箱" />
</div>
<div class="loginbox-textbox hidden">
<input type="text" class="form-control" name="code" placeholder="请输入验证码" />
</div>
<div class="loginbox-submit">
<input type="submit" class="btn btn-primary btn-block" id="verify" value="获取验证码">
</div>
<div class="loginbox-submit hidden">
<input type="submit" class="btn btn-primary btn-block" id="reset" value="重置密码">
</div>
</div>
<div class="logobox">
<p class="text-center" style="font-size: 18px;font-weight: bold;text-shadow: 3px 3px 3px #FF0000;font-style: italic;"></p>
</div>
</form>
</div>
<script src="assets/admin/js/skins.min.js"></script>
<!--Basic Scripts-->
<script src="assets/admin/js/jquery.min.js"></script>
<script src="assets/admin/js/bootstrap.min.js"></script>
<script src="assets/admin/js/slimscroll/jquery.slimscroll.min.js"></script>
<script src="assets/lib/layer/layer.js"></script>
<!--Beyond Scripts-->
<script src="assets/admin/js/beyond.js"></script>
<script>
$('#verify').click(function () {
var data = $('form').serialize();//表单序列化
$.ajax({
url: "{{url('forget')}}",
data: data,
type: 'post',
dataType:'json',
success:function(data){
if (data.code == 1) {
layer.alert(data.msg,{
offset:'220px',
icon:6,
time:2000
},function () {
$('input[name=email]').parent().addClass('hidden');
$('input[name=code]').parent().removeClass('hidden');
$('#verify').parent().addClass('hidden');
$('#reset').parent().removeClass('hidden');
});
} else {
layer.open({
skin:'layui-layer-molv',
offset:'220px',
title:'验证码发送失败',
content:data.msg,
icon: 5,
anim:6
});
}
}
});
return false;
}) //发送验证码
$('#reset').click(function () { //重置密码
var data = $('form').serialize();//表单序列化
$.ajax({
url: "{{url('reset')}}",
data: data,
type: 'post',
dataType:'json',
success:function(data){
if (data.code == 1) {
layer.alert(data.msg,{
offset:'220px',
icon:6,
time:2000
},function () {
window.location.href=data.url;
});
} else {
layer.open({
skin:'layui-layer-molv',
offset:'220px',
title:'重置密码失败',
content:data.msg,
icon: 5,
anim:6
});
}
}
});
return false;
}) //发送验证码
</script>
</body>
</html>
```
## 3.控制器
Controller/admin/Index.php(reister操作方法)
```php
// 忘记密码
public function forget()
{
if (request()->isMethod('post')){
$data=request()->only(['email']);
$res=(new Admin)->getCode($data);
if ($res == 1){
$msg=[
'code'=>1,
'msg'=>'验证码发送成功'
];
}else{
$msg=[
'code'=>1,
'msg'=>$res
];
}
return $msg;
}
return view('admin.index.forget');
}
```
## 4.模型
Models\Admin.php
```php
public function getCode($data)
{
$rules=[
'email'=>'bail|required|email',
];
$msg=[
'email.required'=>'邮箱不能为空',
'email.email'=>'请填写正常邮箱格式',
];
$validate=Validator::make($data,$rules,$msg);
if ($validate->fails()){
return $validate->errors()->first();
}
$res=$this->where('email',$data['email'])->first();
if ($res){
$code=mt_rand(1000,9999);
session(['code'=>$code]);
$content='您的验证码是'.$code;
$sendMail=mailto($data['email'],'获取验证码,$content);
if ($sendMail){
return 1;
}else{
return '发送失败,请等待一会再试~~';
}
}else{
return '该邮箱尚未注册';
}
}
```