今天忽然想写一个用户修改密码的功能,又没有短信接口,只能选择用邮箱发送验证码啦,穷啊,没办法,哈哈,以下为正文。
------------------------------------------------------------------------------------------------------------------------------------------------------------
1、准备工作
(1)下载PHPMailer,插件,下载地址:链接:http://pan.baidu.com/s/1hskrDqs 密码:dcs9
(2)将下载好的文件加压,放在thinkphp扩展库内(个人存放路径:根目录\ThinkPHP\Library\Vendor\PHPMailer)
2、代码拼写
(1)在根目录\项目目录\Common\conf\config.php内,加入以下代码;
// 配置邮件发送服务器 'MAIL_HOST' =>'smtp.163.com',//smtp服务器的名称 'MAIL_SMTPAUTH' =>TRUE, //启用smtp认证 'MAIL_USERNAME' =>'99999999@163.com',//发件人的邮箱名 'MAIL_PASSWORD' =>'9999999',//163邮箱发件人授权密码 'MAIL_FROM' =>'9999999@163.com',//发件人邮箱地址 'MAIL_FROMNAME'=>'某某名',//发件人姓名 'MAIL_CHARSET' =>'utf-8',//设置邮件编码 'MAIL_ISHTML' =>TRUE, // 是否HTML格式邮件
注释:以下参数
发件人的邮箱名,
163邮箱发件人授权密码,
发件人邮箱地址,
发件人姓名
请自行更改(附163邮箱发件人授权密码获取方式)。
(2)在根目录\项目目录\Common\Common内,新建function.php文件,并加入以下代码;
<?php /* * 发送邮件 * @param $to string * @param $title string * @param $content string * @return bool * */ function sendMail($to, $title, $content) { Vendor('PHPMailer.PHPMailerAutoload'); $mail = new PHPMailer(); //实例化 $mail->IsSMTP(); // 启用SMTP $mail->Host=C('MAIL_HOST'); //smtp服务器的名称(这里以QQ邮箱为例) $mail->SMTPAuth = C('MAIL_SMTPAUTH'); //启用smtp认证 $mail->Username = C('MAIL_USERNAME'); //发件人邮箱名 $mail->Password = C('MAIL_PASSWORD') ; //163邮箱发件人授权密码 $mail->From = C('MAIL_FROM'); //发件人地址(也就是你的邮箱地址) $mail->FromName = C('MAIL_FROMNAME'); //发件人姓名 $mail->AddAddress($to,"尊敬的客户"); $mail->WordWrap = 50; //设置每行字符长度 $mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式邮件 $mail->CharSet=C('MAIL_CHARSET'); //设置邮件编码 $mail->Subject =$title; //邮件主题 $mail->Body = $content; //邮件内容 $mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示 return($mail->Send()); }
(3)本人使用的是ajax发送验证码,以下为html代码;
<form method="post" action=""> <input name="forget_name" id="forget_name" placeholder="账号" required="" type="text"> <hr class="hr15"> <input name="forget_email" placeholder="邮箱" id="forget_email" required="" type="text" style="width:70%"> <a href="#" onclick="send();">发送验证码</a> <hr class="hr15"> <input name="code" placeholder="验证码" required="" type="text"> <hr class="hr15"> <input name="new_pass" placeholder="新密码" required="" type="text"> <hr class="hr15"> <input value="发送验证码" style="width:100%;" type="submit"> <hr class="hr20"> <a href="">登录</a> </form> <script> function send(){ var name = $('#forget_name').val(); var email = $('#forget_email').val(); $.ajax({ type:"get", url:"http://127.0.0.1/index.php/Edu/Login/send.html", async:true, contentType:"application/json", dataType:"json", data:{"forget_name":name,"forget_email":email}, success:function(data){ alert(data); } }); } </script> <script src="__PUBLIC__/Admin/style/jquery.min.js"></script>
(4)以下为php代码;
//发送邮件验证码 public function send(){ if(IS_AJAX){ //接收账号信息 $data['name'] = I('get.forget_name'); if(!$data['name']) $this->ajaxReturn("请填写登录账号",0); //接收邮箱 $data['email'] = I('get.forget_email'); if(!$data['email']) $this->ajaxReturn("请填写邮箱账号",0); //验证邮箱格式 $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i"; if(preg_match( $pattern, $data['email'])){ //邮箱格式正确下,判断用户是否存在 $Manager = D('Manager'); $condition['display'] = 1; $condition['name'] = $data['name']; $condition['status'] > 1; $ManagerInfo = $Manager->where($condition)->find(); if($ManagerInfo){ //核对用户账号与邮箱是否一致 if($ManagerInfo['email'] == $data['email']){ //设置参数 $title = "邮箱验证"; //邮件标题 $font = "此邮件为找回密码验证邮件,请勿回复,您的验证码为"; //邮件内容 $code = rand(1000,9999); //随机码 $content = $font.$code; //拼接 if(SendMail($data['email'],$title,$content)) { //存验证码 $Condition['code'] = $code; $Condition['updatetime'] = time(); $Cond['name'] = $data['name']; $ManagerSave = $Manager->where($Cond)->save($Condition); if($ManagerSave){ $this->ajaxReturn("发送成功",0); }else{ $this->ajaxReturn("验证码保存失败",0); } }else{ $this->ajaxReturn("发送失败",0); } }else{ $this->ajaxReturn("请核对用户信息的正确性",0); } }else{ $this->ajaxReturn("请核对用户信息",0); } }else{ $this->ajaxReturn("请核对邮箱信息",0); } }else{ $this->ajaxReturn(0); } }
(5)QQ邮箱验证码接收情况;