您的位置:首页 > 编程语言 > PHP开发

在yii 框架下 使用phpmailer 通过邮箱验证找回密码功能

2015-01-15 17:44 656 查看
在yii框架下,找回密码功能

1、在下载phpmailer 扩展文件压缩包http://url.cn/SRdQNW

放置在protected/extensions文件夹下

2、在配置文件main.php

'mail'=>array(
'class'=>'ext.PHPMailer.phpmailer',
'CharSet' => 'utf-8', // 您的企业邮局域名
'Host' => 'smtp.qq.com', // 您的企业邮局域名
'SMTPAuth' => true, // 启用SMTP验证功能
'Username' => 'xx@xx.com', // 邮局用户名(请填写完整的email地址)
'Password' => 'xxxx', // 邮局密码
'Port'=>25,
'From' => 'xx@xx.com', //邮件发送者email地址
'FromName' => 'xxx',
),
3、写view页面

3、1 forgetpwd页面(部分HTML代码)

<section class="user_info" id="userInfo">

<form id="forgetPwdForm"
action="<?php echo CHtml::normalizeUrl(array("forgetpwd/forgetpwd")); ?>"
method="post">
<div class="user_s">
<div class="user_title">请输入您注册的电子邮箱,找回密码</div>
<table>

<tr class="form-group">
<td class="user_l">邮    箱</td>
<td class="user_r"><input type="email" id="email"
name="email" data-vtype="required email" />
<div class="user_ts">邮箱地址不能为空</div></td>
</tr>

<tr>
<td class="user_l"></td>
<td class="user_r"><a href="javascript:void(0);"
class="user_submit" onclick="saveApply()">确认提交</a></td>
</tr>
</table>

</div>
</form>
</section>

<div class="succ_info" id="succInfo" style="display: none">

<div class="succ_title">提交成功!</div>

<div class="succ_con">请登录邮箱查收电子邮件,找回密码!</div>
<a href="<?php echo CHtml::normalizeUrl(array("site/index")); ?>"
class="succ_gohome">>>返回首页</a>
</div>


3、2 resetpwd页面

<section class="user_info" id="userInfo">

<form id="resetpwdForm"
action="<?php echo CHtml::normalizeUrl(array("resetpwd/resetpwd")); ?>"
method="post">
<div class="user_s">
<div class="user_title">重置密码</div>
<table>
<tr class="form-group">

<td class="user_r"><input type="hidden" name="hidden1"
value="<?php echo $email = trim ( $_GET ['token'] );?>" />//隐藏域表单传递通过$_GET获取到的数据
</td>
</tr>
<tr class="form-group">

<td class="user_r"><input type="hidden" name="hidden2"
value="<?php echo $email = trim ( $_GET ['email'] );?>" />
</td>
</tr>
<tr class="form-group">
<td class="user_l">新  密  码</td>
<td class="user_r"><input onchange="checkPasswords()"
oninput="setCustomValidity('')" required type="password"
name="password" id="password" data-vtype="required" minlength="6"
maxlength="32" />
<div class="user_ts">密码长度必须是6-32位</div></td>
</tr>
<tr class="form-group">
<td class="user_l">重复密码</td>
<td class="user_r"><input onchange="checkPasswords()"
type="password" name="repassword" id="repassword"
data-vtype="required" maxlength="32" />
<div class="user_ts">再次输入密码</div></td>
</tr>

<tr>
<td class="user_l"></td>
<td class="user_r"><a href="javascript:void(0);"
class="user_submit" onclick="saveApply()">确认提交</a></td>
</tr>
</table>

</div>
</form>
</section>

<div class="succ_info" id="succInfo" style="display: none">

<div class="succ_title">修改成功!</div>

<div class="succ_con">修改成功,请登录!</div>
<a href="<?php echo CHtml::normalizeUrl(array("site/index")); ?>"
class="succ_gohome">>>返回首页</a>
</div>


4、在site控制器中渲染视图

/**
* 忘记密码
*
*/
public function actionForgetPwd() {
$this->render ( "forgetpwd" );
}
/**
* 重置密码
*
*/
public function actionResetPwd() {

$this->render ( "resetpwd" );
}
5、写forgetpwd控制器

<?php
class ForgetpwdController extends CController {
public function actionForgetpwd() {

$email = stripslashes(trim($_POST ['email']));
if ($email) {
$usercount = XXX::model ()->count ( "email=:EMAIL", array (
":EMAIL" => $email
) );
if ($usercount == 0) {
echo json_encode ( array (
"ret" => 1,
"msg" => "该电子邮箱尚未注册!"
) );
exit ();
} else {
$userinfo = <span style="font-family: Arial, Helvetica, sans-serif;">XXX</span>::model ()->find ( "email=:EMAIL", array (
":EMAIL" => $email
) );
$getpasstime = time ();
$id = $userinfo ['id'];
$token = md5 ( $id . $userinfo ['loginname'] . $userinfo ['password'] ); // 组合验证码
$url = "http://www.xxx.com/site/resetpwd?email=" . $email . "&token=" . $token;
$time = date ( 'Y-m-d H:i' );

$mail = Yii::App()->mail;
$mail->IsSMTP();
$mail->AddAddress("$email");
$mail->Subject = @"密码修改通知"; //邮件标题
$mail->Body = @"亲爱的" . $email . ":您在" . $time . "提交了找回密码请求。
请点击下面的链接重置密码(企业邮箱,请勿回复此邮件):$url"; //邮件内容
if(!$mail->Send())
{
echo $mail->ErrorInfo;

}else{
// return true;
}
echo json_encode ( array (
"ret" => 0,
"msg" => "提交成功"
) );

//echo $msg;
}
}
}
}
?>


6、resetpwd 控制器

<?php
class ResetpwdController extends CController {
public function actionResetpwd() {

$token = trim ( $_POST ['hidden1'] );
$email = trim ( $_POST ['hidden2'] );

$userinfo = XXX::model ()->find ( "email=:EMAIL", array (
":EMAIL" => $email
) );

if ($userinfo) {
$mt = md5 ( $userinfo ['id'] . $userinfo ['loginname'] . $userinfo ['password'] );
if ($mt == $token) {

if (isset ( $_POST ['password'] )) {
$id = $userinfo ['id'];
$pwd=<span style="font-family: Arial, Helvetica, sans-serif;">XXX</span><span style="font-family: Arial, Helvetica, sans-serif;">::model()->find ( "id=:ID", array (</span>
":ID" => $id
) );
$pwd->password=sha1($_POST ['password']);
$pwd->save(); // 将更改保存到数据库

if ($pwd) {
echo json_encode ( array (
"ret" => 0,
"msg" => "修改成功,请重新登录"
) );
exit ();

} else {
echo json_encode ( array (
"ret" => 1,
"msg" => "修改失败"
) );
exit ();
}
}

} else {
echo json_encode ( array (
"ret" => 1,
"msg" => "无效的链接"
) );

}
} else {
echo json_encode ( array (
"ret" => 1,
"msg" => "错误的链接!"
) );
}

}
}

?>


来源:http://myfetish.cn/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php yii 密码 邮件 email