下面給大家介紹關(guān)于thinkphp郵箱驗(yàn)證碼前后臺,希望對需要的朋友有所幫助!
thinkphp郵箱驗(yàn)證碼前后臺
思路:
前臺用jquery.validate驗(yàn)證,驗(yàn)證郵箱是否存在等等,交互用ajax來做。后端用郵箱類庫直接上代碼吧
準(zhǔn)備:
1、下載PHPMailer類庫,放到Application/Thinkphp/Library/Vendor/中,在網(wǎng)上搜搜,搜不到可以聯(lián)系我
2、開啟SMTP,舉個網(wǎng)易郵箱的例子
config.php里填寫:
// 配置郵件發(fā)送服務(wù)器 'MAIL_HOST' =>'smtp.163.com',//smtp服務(wù)器的名稱 'MAIL_SMTPAUTH' =>TRUE, //啟用smtp認(rèn)證 'MAIL_USERNAME' =>'lishine0216@163.com',//你的郵箱名 'MAIL_FROM' =>'lishine0216@163.com',//發(fā)件人地址 'MAIL_FROMNAME'=>'南京商丘商會',//發(fā)件人姓名 'MAIL_PASSWORD' =>'a3392894',//郵箱密碼 'MAIL_CHARSET' =>'utf-8',//設(shè)置郵件編碼 'MAIL_ISHTML' =>TRUE, // 是否HTML格式郵件
function.php填寫
<?php /** * 郵件發(fā)送函數(shù) */ function sendMail($to, $title, $content) { Vendor('PHPMailer.PHPMailerAutoload'); $mail = new PHPMailer(); //實(shí)例化 $mail->IsSMTP(); // 啟用SMTP $mail->Host=C('MAIL_HOST'); //smtp服務(wù)器的名稱(這里以QQ郵箱為例) $mail->SMTPAuth = C('MAIL_SMTPAUTH'); //啟用smtp認(rèn)證 $mail->Username = C('MAIL_USERNAME'); //你的郵箱名 $mail->Password = C('MAIL_PASSWORD') ; //郵箱密碼 $mail->From = C('MAIL_FROM'); //發(fā)件人地址(也就是你的郵箱地址) $mail->FromName = C('MAIL_FROMNAME'); //發(fā)件人姓名 $mail->AddAddress($to,"尊敬的客戶"); $mail->WordWrap = 50; //設(shè)置每行字符長度 $mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式郵件 $mail->CharSet=C('MAIL_CHARSET'); //設(shè)置郵件編碼 $mail->Subject =$title; //郵件主題 $mail->Body = $content; //郵件內(nèi)容 $mail->AltBody = "這是一個純文本的身體在非營利的HTML電子郵件客戶端"; //郵件正文不支持HTML的備用顯示 //return($mail->Send()); if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } }
后端:
public function createSMSCode($length = 6){ $min = pow(10 , ($length - 1)); $max = pow(10, $length) - 1; return rand($min, $max); } //ajax發(fā)送郵件 public function e_mail_send(){ $e_mail=I("post.e_mail"); $result=M("user")->where(array("e_mail"=>$e_mail))->find(); if($result){ $code=$this->createSMSCode(); //存儲驗(yàn)證碼,相當(dāng)于全局變量,因?yàn)樵俅握{(diào)用createSMSCode時,代碼會變 session("code",$code); session("e_mail",$e_mail); SendMail($e_mail,"南京商丘商會驗(yàn)證碼","您的驗(yàn)證碼是".$code.",請正確填寫"); $data="yes"; }else{ $data="no"; } $this->ajaxReturn($data); }
前端:
<p class="fot_main"> <p class="container"> <p class="row"> <p class="my_ar"> <h3>密碼找回!<br/><span>請?zhí)钊肽泥]箱進(jìn)行密碼找回</span><br/><span>發(fā)送后請注意查收郵箱信息填入驗(yàn)證碼中</span></h3> <form method="post" action="{:U('Forget/forget_do')}"> <label>請輸入注冊郵箱:</label><input type="text" name="e_mail" id="e_mail" class="fot_name"> <a href="javascript:;" id="send" class="fasong" style="display: inline-block;width:70px;height:29px;background:#f3980f;;color:#fff;line-height: 29px;text-align: center;border:1px solid #919191;">發(fā)送</a><br/> <p style="margin-left:130px;color:orange;" id="back"></p> <label>驗(yàn)證碼:</label><input type="text" name="code" class="yan"/><br/> <!--<ul class="yanzhengma"> <li>w</li> <li>z</li> <li>g</li> <li>4</li> </ul> <span>看不清?</span> <span>換一張</span><br/>--> <label>新密碼:</label><input type="text" name="npwd" class="xin_password"/><br/> <label>確認(rèn)密碼:</label><input type="text" name="renpwd" class="xin_password"/><br/> <p class="ccc"><input type="submit" value="提交" class="sub"></p> </form> </p> </p> </p> </p>
ajax部分:
<script> $("#send").click(function(){ var e_mail=$("#e_mail").val(); $.post("{:U('Forget/e_mail_send')}","e_mail="+e_mail,function(data){ if(data=="yes"){ $("#back").html("發(fā)送成功,請查收郵件"); }else{ $("#back").html("郵箱錯誤或不存在"); } }); }); </script>