码迷,mamicode.com
首页 > 其他好文 > 详细

发送邮箱实现找回密码

时间:2015-08-15 10:23:29      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:phpmailer   发送邮件   找回密码   邮件链接有效期   

注意事项

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>

 

 

 

   

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

发送邮箱实现找回密码

标签:phpmailer   发送邮件   找回密码   邮件链接有效期   

原文地址:http://blog.csdn.net/xiesumin/article/details/47679473

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!