美文网首页
TP框架 发送邮箱实现找回密码

TP框架 发送邮箱实现找回密码

作者: ssmsmall | 来源:发表于2017-03-02 17:52 被阅读0次

    注意事项

    开启socket:在PHP.ini中取消extension=php_sockets.dll前面的分号。

    .登录你的邮箱手动开启STMP服务,这个服务默认是关闭的,一定要去邮箱->设置里去手动开启,开启时要求你设置一个独立密码,这个密码就是写在 'MAIL_PASSWORD'=>' ', // 邮箱密码,的密码而不是邮箱登录密码

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

    1、      在ThinkPHP/Extend/Library/ORG/Net/目录下放入PHPMailer.class.php、class.pop3.php、class.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文件中建一个函数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 'alert("该用户不存在或者邮箱不正确!");location.href="http://localhost/jiaxiao2/index.php/Index/findpwd"';      } 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'alert("系统已将重置密码的链接安全的发到了您的邮箱,请及时查收!");location.href="http://localhost/jiaxiao2/index.php/?n=1"';          } else {                echo'alert("邮件发送失败!");location.href="http://localhost/jiaxiao2/index.php/Index/findpwd"';          }      }  }    //找回密码  //邮箱有效期  从数据库取出发送邮件的提交时间,和当前时间做对比,如果当前时间超过提交时间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'alert("修改密码成功!");location.href="http://localhost/jiaxiao2/index.php/?n=1"';          }      } else {          echo 'alert("该链接已经过期!");location.href="http://localhost/jiaxiao2/index.php/?n=1"';      }      $this->display();}

    5、        然后在在Index/Tpl/Index中加入以下两个html文件Findpwd.html找回密码

    返回用户名

    邮箱

    Findpassword.html文件找回密码

    返回

    用户名

    新的密码

    确认新密码

    6、        然后在登录页面加上相应的链接在Index/Tpl/common/top.html和indextop.html中学员和教练后面加上忘记登录密码?

    相关文章

      网友评论

          本文标题:TP框架 发送邮箱实现找回密码

          本文链接:https://www.haomeiwen.com/subject/shskgttx.html