发送邮箱实现找回密码

注意事项

1.开启socket:在php.ini中取消extension=php_sockets.dll前面的分号。
2.
登录你的邮箱手动开启STMP服务,这个服务默认是关闭的,一定要去邮箱->设置里去手动开启,开启时要求你设置一个独立密码,这个密码就是写在 'MAIL_PASSWORD'=>' ', // 邮箱密码,的密码而不是邮箱登录密码

3userinfo中加上一个字段Resettime(邮箱提交时间,和当前时间比较,判断链接是否过期)

1      ThinkPHP/Extend/Library/ORG/Net/目录下放入PHPMailer.class.phpclass.pop3.phpclass.smtp.php文件

2、        Index/Conf/config.php配置文件中加上以下配置项

//发送邮件

   'MAIL_ADDRESS' => '18782227612@163.com', // 邮箱地址(注册时好像会有一个地址)

   'MAIL_SMTP' => 'smtp.163.com', // 邮箱SMTP服务器

   'MAIL_LOGINNAME' => 'm18782227612_2@163.com', // 邮箱登录帐号

   'MAIL_PASSWORD' => 'bviqbapkcyyrrtmf', // 邮箱密码(这是我开启STMP服务时给的授权密码,注意看注意事项第2点)

   'MAIL_CHARSET' => 'UTF-8', // 编码

   'MAIL_AUTH' => true, // 邮箱认证

   'MAIL_HTML' => true, // true HTML格式 false TXT格式

3、         Index/Common /common.php文件中建一个函数

<?php

functionSendMail($address,$title,$message)

{

import('ORG.Net.PHPMailer');//导入类

$mail=new PHPMailer();

// 设置PHPMailer使用SMTP服务器发送Email

$mail->IsSMTP();

// 设置邮件的字符编码,若不指定,则为'UTF-8'

$mail->CharSet='UTF-8';

// 添加收件人地址,可以多次使用来添加多个收件人

$mail->AddAddress($address);

// 设置邮件正文

$mail->Body=$message;

// 设置邮件头的From字段。

$mail->From=C('MAIL_ADDRESS');

// 设置发件人名字

$mail->FromName='zyimm';

// 设置邮件标题

$mail->Subject=$title;

// 设置SMTP服务器。

$mail->Host=C('MAIL_SMTP');

// 设置为“需要验证”

$mail->SMTPAuth=true;

// 设置用户名和密码。

$mail->Username=C('MAIL_LOGINNAME');

$mail->Password=C('MAIL_PASSWORD');

// 发送邮件。

return($mail->Send());

}

 

4、        Index/Lib/Action/IndexAction.class.php中加入以下两个方法

  //找回密码---发送到邮箱,用户验证

   public function sendemail() {

       import('ORG.Net.PHPMailer');

       $r = M('userinfo')->where(array('Loginname' => I('username'),'Email' => I('email')))->find();

       $this->user = $r;

       if (!$r) {

           echo '<script>alert("该用户不存在或者邮箱不正确!");location.href="http://localhost/jiaxiao2/index.php/Index/findpwd"</script>';

       } else {

           $user = I('username');

           $email = I('email');

           $content = "$user,你好:

              您收到这封电子邮件是因为您 (也可能是某人冒充您的名义) 申请了一个找回密码的请求。

       假如这不是您本人所申请, 或者您曾持续收到这类的信件骚扰, 请您尽快联络管理员。

       您可以点击如下链接重新设置您的密码,如果点击无效,请把下面的代码拷贝到浏览器的地址栏中:

       http://localhost/jiaxiao2/index.php/Index/findpassword?Loginname=$user

       在访问链接之后, 您可以重新设置新的密码。";

           $rs = SendMail($email, '民大驾校---用户密码找回', $content,'manager'); //SendMail('admin@waikucms.com','邮件标题','邮件正文','歪酷CMS管理员');解释下参数: 参数1---目标邮箱, 参数2----邮件标题,参数三--邮件正文,参数四---发件人名称;

           if ($rs) {

               M('userinfo')->where(array('Loginname' => I('username'), 'Email'=> I('email')))->save(array('Resettime' => date('Y-m-d H:i:s')));

                echo'<script>alert("系统已将重置密码的链接安全的发到了您的邮箱,请及时查收!");location.href="http://localhost/jiaxiao2/index.php/?n=1"</script>';

           } else {

                echo'<script>alert("邮件发送失败!");location.href="http://localhost/jiaxiao2/index.php/Index/findpwd"</script>';

           }

       }

   }

 

   //找回密码

   //邮箱有效期   从数据库取出发送邮件的提交时间,和当前时间做对比,如果当前时间超过提交时间30分,则过期

   public function findpassword(){

       $username = I('Loginname');

       $user = M('userinfo')->where(array('Loginname' =>$username))->select();

       $this->user = $user;

       $u = M('userinfo')->where(array('Loginname' =>$username))->find();

       $resettime = $u['Resettime']; //获取数据库邮箱发送时间

       $time = date("Y-m-d H:i:s", strtotime("-30 min"));//当前时间减去30分后还小于等于发送邮箱时间,则时间有效,反之过期

       $data = array('Password' => md5(I('password')));

       $rs = M('userinfo')->where(array('Loginname' => I('Loginname')))->save($data);

       if ($resettime >= $time) {

           if ($rs) {

                echo'<script>alert("修改密码成功!");location.href="http://localhost/jiaxiao2/index.php/?n=1"</script>';

           }

       } else {

           echo '<script>alert("该链接已经过期!");location.href="http://localhost/jiaxiao2/index.php/?n=1"</script>';

       }

       $this->display();

}

5、        然后在在Index/Tpl/Index中加入以下两个html文件

Findpwd.html

<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

   <head>

       <meta charset="UTF-8">

           <title>找回密码</title>

           <meta name="viewport"content="width=device-width">

                <link rel="stylesheet"type="text/css" href="__PUBLIC__/css/reg.css" />

                <scripttype="text/javascript"src="__PUBLIC__/js/jquery.js"></script>

                <scripttype="text/javascript"src="__PUBLIC__/js/jquery.ui.js"></script>

               <scripttype="text/javascript"src="__PUBLIC__/js/jquery.validate.js"></script>

                <scripttype="text/javascript"src="__PUBLIC__/js/jquery.form.js"></script>

                <scripttype="text/javascript" src="__PUBLIC__/js/reg.js"></script>                                     

                  <scripttype="text/javascript">

                        functionrefreshVerify() {

                           document.getElementById('reverifyImg').src = '__URL__/reverify/' +Math.random();

                        }

                </script>

                </head>

                <body>

                    <divclass="rwhole">

                        <divclass="rtop">

                            <p>找回密码</p>

                            <span>

                                <ahref="{:U('Index/Index/index')}?n=1">返回</a>

                            </span>

                        </div>

                        <divclass="rcontent">

                            <divclass="rcontent_left">

                                <formclass="reg" id="reg"action="{:U('sendemail')}" method="get" >

                                    <p>

                                       <span>用户名</span>

                                       <input class="inp" type="text"name="username" id="username" />

                                    </p>

                                    <p>

                                       <span>邮箱</span>

                                       <input  class= "inp"type="text" name="email" id="pass" />

                                    </p>

                                    <p><input style="margin-left:95px;margin-top: 20px;width: 155px;height:36px;border: 1px solid #fff;" type="Submit"name="Submit" value="提交"/></p>                            

                                </form>

                            </div>

                        </div>

                        <divclass="footer"></div>

                    </div>

                </body>

                </html>

Findpassword.html文件

<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

   <head>

       <meta charset="UTF-8">

           <title>找回密码</title>

           <meta name="viewport"content="width=device-width">

                <linkrel="stylesheet" type="text/css"href="__PUBLIC__/css/reg.css" />

                <scripttype="text/javascript"src="__PUBLIC__/js/jquery.js"></script>

               <scripttype="text/javascript"src="__PUBLIC__/js/jquery.ui.js"></script>

                <scripttype="text/javascript"src="__PUBLIC__/js/jquery.validate.js"></script>

                <scripttype="text/javascript" src="__PUBLIC__/js/jquery.form.js"></script>

                <scripttype="text/javascript"src="__PUBLIC__/js/reg.js"></script>                                     

                  <scripttype="text/javascript">

                        functionrefreshVerify() {

                            document.getElementById('reverifyImg').src= '__URL__/reverify/' + Math.random();

                        }

                </script>

                </head>

                <body>

                    <divclass="rwhole">

                        <divclass="rtop">

                            <p>找回密码</p>

                            <span>

                                <ahref="{:U('Index/Index/index')}?n=1">返回</a>

                            </span>

                        </div>

                        <div class="rcontent">

                            <divclass="rcontent_left">

                                <formclass="reg" id="reg"action="{:U('findpassword')}" method="get" >

                                    <p>

                                       <volist name="user" id="user">

                                       <span>用户名</span>

                                       <input class="inp" type="text"name="Loginname" id="username"value="{$user.Loginname}" />

                                     </volist> 

                                    </p>

                                    <p>

                                       <span>新的密码</span>

                                       <input  class= "inp"type="password" name="password" id="pass" />

                                    </p>

                                    <p>

                                       <span>确认新密码</span>

                                       <input class="inp" type="password"name="repass" id="repass" />

                                    </p>

                                    <p><input style="margin-left:95px;margin-top: 20px;width: 155px;height:36px;border: 1px solid #fff;" type="Submit"name="Submit" value="提交"/></p>                            

                                </form>

                            </div>

                        </div>

                        <divclass="footer"></div>

                    </div>

                </body>

                </html>

 

 

6、        然后在登录页面加上相应的链接

在Index/Tpl/common/top.html和indextop.html中学员和教练后面加上

               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<ahref="{:U('Index/Index/findpwd')}">忘记登录密码?</a>

 

 

 

   

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值