<?php
require_once('header.php');
require_once("classes/Smtp.class.php");

$username = flc_request(trim($_POST["username"]));
$username = strtolower($username); //转成小写
$ip=GetUserIP(); 

$sql = "select * from {$tablepre}scuser where UserName='".$username."'";
$row = $db->get_row($sql);

if (empty($row)){
   $response_arr = array("status" => 0,"message"  => $username.'帐号不存在!');
   echo json_encode($response_arr);
   exit;
}else{
      $pwd=randomchar(6);//生成随机密码
      $smtpserverport = 25;//SMTP服务器端口
      $mailtitle = $site_name."用户密码取回结果通知";
      $mailbody = "亲爱的".$username.":<br><br>您取回的新密码是：".$pwd;
      $mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件

	  $smtp = new Smtp($smtpserver,$smtpserverport,true,$smtpusermail,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
	  $smtp->debug = false;//是否显示发送的调试信息
	  $state = $smtp->sendmail($username, $smtpusermail, $mailtitle, $mailbody, $mailtype);
      if($state!=""){
	     $db->exec("update {$tablepre}scuser set Password='".md5($pwd)."',isCheck=1 where UserName='".$username."'");
		 $response_arr = array("status" => 1,"message"  => '密码已发送到你的邮箱,请查收!');
         echo json_encode($response_arr);
         exit;
	  }else{
		 $response_arr = array("status" => 0,"message"  => '邮件服务器异常,取回密码失败,请联系网站管理员！');
         echo json_encode($response_arr);
         exit;
	  }

}
?>