一、PHPMailer、composer下载以及使用
下载连接
https://github.com/PHPMailer/PHPMailer
https://getcomposer.org/Composer-Setup.exe
PHPMailer使用
将src中的PHPMailer.php、SMTP.php、Exception.php放在项目文件之中
检查php中openssl和sockets是否开启
cmd 命令 php -m
检查php环境
cmd 命令 php -v
二、邮箱开启POP3/SMTP/IMAP服务
记住授权码后面会用
三、代码
1.前端界面1
<!doctype html>
<html lang="en">
<head>
<meta charset="">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="box">
<div class="top">
</div>
<form action="reset_password.php" method="POST">
<input type="text" name="user" placeholder="用户名" class="text" value="<?php echo empty($_COOKIE["user"])?"":$_COOKIE["user"]?>">
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" value="<?php echo empty($_COOKIE["email"])?"":$_COOKIE["email"]?>" required>
<button type="submit">确定</button>
</form>
<div>
<div class="bottom">
</div>
</div>
</div>
<script type="text/javascript">
function check(){
}
</script>
<style type="text/css">
body{
padding: 0;
margin: 0;
/*background-image: linear-gradient(to bottom,lightblue,cornflowerblue);*/
display: flex; /*弹性布局*/
justify-content: center;
align-items: center;
height: 100vh;
background-image: url("path/bg1.png");
}
.box{
width: 240px;
height: 500px;
/*background-color: white;*/
padding: 0 50px ;
border-radius: 15px;/*圆角边框*/
}
.top{
font-size: 35px;
color: red;
font-weight: bold;
text-align: center;
line-height: 170px;
}
.text{
width: 100%;
border: 0;
border-bottom: solid 1px papayawhip;
font-size: 20px;
margin-bottom: 30px ;
outline: none;
}
.button{
width: 100%;
border: 0;
font-size: 25px;
color: wheat;
background-image: linear-gradient(to right,palevioletred,indianred);
cursor:pointer; /*光标的类型*/
}
.bottom{
line-height: 60px;
font-size: 20px;
text-align: center;
}
.expire{
font-size: 20px;
color: #1eacae;
cursor: pointer;
}
</style>
</body>
</html>
前端界面2
<!doctype html>
<html lang="en">
<head>
<meta charset="">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="box">
<div class="top">
登录
</div>
echo '发送成功';
<div>
<div class="bottom">
</div>
</div>
</div>
<style type="text/css">
body{
padding: 0;
margin: 0;
/*background-image: linear-gradient(to bottom,lightblue,cornflowerblue);*/
display: flex; /*弹性布局*/
justify-content: center;
align-items: center;
height: 100vh;
background-image: url("path/bg1.png");
}
.box{
width: 240px;
height: 500px;
/*background-color: white;*/
padding: 0 50px ;
border-radius: 15px;/*圆角边框*/
}
.top{
font-size: 35px;
color: red;
font-weight: bold;
text-align: center;
line-height: 170px;
}
.text{
width: 100%;
border: 0;
border-bottom: solid 1px papayawhip;
font-size: 20px;
margin-bottom: 30px ;
outline: none;
}
.button{
width: 100%;
border: 0;
font-size: 25px;
color: wheat;
background-image: linear-gradient(to right,palevioletred,indianred);
cursor:pointer; /*光标的类型*/
}
.bottom{
line-height: 60px;
font-size: 20px;
text-align: center;
}
.expire{
font-size: 20px;
color: #1eacae;
cursor: pointer;
}
</style>
</body>
</html>
2.脚本语言
<?php
use PHPMailer\PHPMailer\PHPMailer;
//使用自己的文件路径
require 'path/Exception.php';
require 'path/PHPMailer.php';
require 'path/SMTP.php';
//改成自己的数据库
$conn=mysqli_connect("localhost","root","root","new");
$email = mysqli_real_escape_string($conn, $_POST['email']);
//检查邮箱是否存在数据库中
$sql = "SELECT * FROM user WHERE email='$email'";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result)<=0){
$_SESSION['status'] = "账号不存在";
}else{
//生成并保存验证码
$code = rand(1000,9999);//生成随机验证码
$sql = "INSERT INTO reset_password (email, code) VALUES ('$email', '$code')";
mysqli_query($conn, $sql);
$title="We heard that you lost your password ";
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->SMTPAuth = true;
//163的邮箱SMTP格式可以换成其它邮箱格式
$mail->Host = 'smtp.163.com';
//自己的邮箱
$mail->Username = 'xxxxx@163.com';
// 这个就是之前得到的授权码
$mail->Password = 'xxxxxxxxxxxxx';
//设置使用ssl加密方式登录鉴权
$mail->SMTPSecure = 'ssl';
// //设置ssl连接smtp服务器的远程服务器端口号,可选465或587
$mail->Port = 465;
//设置发送的邮件的编码 也可选 GB2312
$mail->CharSet = 'UTF-8';
//发送放的邮箱也就是自己的邮箱
$mail->setFrom('xxxxxxx@163.com', '标题');
$mail->addAddress($email);
//邮件正文是否为html编码 注意此处是一个方法 不再是属性 true或false
$mail->isHTML(true);
// 该邮件的主题
$mail->Subject = $title;
// 该邮件的正文内容
$mail->Body = $code;
$mail->send();
echo '发送成功';
}catch (\Exception $exception){
echo '邮件发送失败: ', $mail->ErrorInfo;
}
header("Location: reset2.php");
}
}
?>