• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

PHP密码重置,发送邮件,随机长度字母数字密码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 1 <?php
 2 include ("database.php");
 3 require_once ('email.class.php'); 
 4 date_default_timezone_set("PRC");
 5 class mysqlJi{//数据库
 6     
 7     static function mysqlQu($sql){//sql语句
 8         return mysql_query($sql);        
 9     }
10     static function mysqlFeAs($result){//遍历结果
11         $i=0;//变量i
12         while($row = mysql_fetch_assoc($result)){
13         $reslist[$i] =$row;
14         $i++;
15         }
16         return $reslist;    
17     }
18     static function jsonEn($reslist){//输出json
19         echo json_encode($reslist);
20     }
21     static function mysqlRows(){ //受影响的行数
22         $info_str = mysql_info(); //函数返回最近一条查询的信息。如果成功,则返回有关该语句的信息,如果失败,则返回 false
23         $a_rows = mysql_affected_rows(); //受影响的行数
24         preg_match("([0-9]*)", $info_str, $r_matched); 
25         return ($a_rows < 1)?($r_matched[1]?$r_matched[1]:0):$a_rows; 
26          
27     }
28     static function mysqlCl(){//关闭数据库
29         mysql_close();
30     }
31 }
32 //忘记密码
33 $email=htmlspecialchars(trim($_POST['email']));//获取邮箱
34 //密码生成
35 function get_password($length) 
36 {
37     $str = substr(md5(time()), 0, $length);
38     return $str;
39 }
40 $pass=get_password(8);
41 $password=md5(sha1($pass).'Mh-(Sg3b8wj?;nDaHlm`)PRIcz>@0}f^/%"|C][:');//设置密码
42     //########################################## 
43                                     $smtpserver = "smtp.qq.com";//SMTP服务器 
44                                     $smtpserverport =25;//SMTP服务器端口 
45                                     $smtpusermail = "[email protected]";//SMTP服务器的用户邮箱 
46                                     $smtpemailto = $email;//发送给谁 
47                                     $smtpuser = "[email protected]";//SMTP服务器的用户帐号 
48                                     $smtppass = "";//SMTP服务器的用户密码 
49                                     $mailsubject = "北京信息网活动-新密码";//邮件主题 
50                                     $mailbody = '
51                                     <table style="text-align:left;border:1px solid #ddd;padding:20px 0" cellpadding="0" cellspacing="0" width="600"><tbody><tr><td><table style="border-bottom:#f60 2px solid;padding-bottom:12px" align="center" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td style="line-height:22px;padding-left:20px"><h1 class="navbar-brand" style="box-sizing:border-box;margin:0;line-height:20px;height:50px;padding:15px 2px;text-transform:uppercase"><span style="box-sizing:border-box;background-color:transparent;text-decoration:none;-webkit-transition:all .3s ease-out 0s;transition:all .3s ease-out 0s;outline:0"><span style="font-size:xx-large;color:#333">北京信息网活动-密码重置</span></span></h1></td><td style="font-size:12px;padding-right:20px;padding-top:30px" align="right">&nbsp;</td></tr></tbody></table><table style="padding:0 20px" align="center" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td style="font-size:14px;color:#333;height:40px;line-height:40px;padding-top:10px">亲爱的<b style="color:#333;font-family:Arial">会员</b> :</td></tr><tr><td style="font-size:12px;line-height:22px"><p style="text-indent:2em;padding:0;margin:0"><span style="color:#333">您好!您的新密码是(</span><span style="color:#f0f">'.$pass.'</span><span style="color:#333">)</span></p></td></tr><tr><td style="padding-top:15px"><span style="color:#000"><a href="http://active.bjxxw.com/" target="_blank" style="display:inline-block;padding:0 25px;height:28px;line-height:28px;text-align:center;background:#f70 none repeat scroll 0 0;font-size:12px;cursor:pointer;border-radius:2px;text-decoration:none" title="立即去官网怎么样">去看看新活动</a></span></td></tr><tr><td style="font-size:12px;color:#333;line-height:22px;padding-top:15px">如果您有任何疑问,请与我们联系。</td></tr><tr><td style="font-size:12px;color:#333;line-height:22px">客服热线:400-0098-987(周一至周五 9:00-18:00)</td></tr><tr><td style="font-size:12px;color:#333;line-height:22px">客服邮箱:<a href="mailto:[email protected]" target="_blank" style="color:#2af">[email protected]</a></td></tr></tbody></table><table style="margin-top:60px" align="center" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td style="font-size:12px;color:#777;line-height:22px;border-bottom:#2af 2px solid;padding-bottom:8px;padding-left:20px">此邮件由系统自动发出,请勿回复!</td></tr><tr><td style="font-size:12px;color:#333;line-height:22px;padding:8px 20px 0">感谢您对北京信息网(<a href="http://www.bjxxw.com/" target="_blank" style="color:#2af;font-family:Arial">http://www.bjxxw.com</a> )的支持,祝您好运!</td></tr><tr><td style="font-size:12px;color:#333;line-height:22px;padding:0 20px">技术支持:北京信息网</td></tr></tbody></table></td></tr></tbody></table><table style="text-align:center" cellpadding="0" cellspacing="0" width="600"><tbody><tr><td style="font-size:12px;color:#999;line-height:30px" align="center">Copyright © 2005 - 2016, 版权所有 bjxxw.com</td></tr></tbody></table>
52                                     ';//邮件内容                                 
53                                     $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件 
54                                     $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. 
55                                     $smtp->debug = false;//是否显示发送的调试信息                                     
56                             
57 $cues = '';//提示语
58 if($email == ''){//邮箱不为空
59         $cues='邮箱不为空';
60     }else{
61             $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
62             if(preg_match($pattern, $email)){
63                 $res=mysqlJi::mysqlQu("SELECT id FROM AAA WHERE email='{$email}'");//sql语句
64                 $rows=mysqlJi::mysqlRows();//受影响的行数
65                 if($rows==1){
66                     $res2=mysqlJi::mysqlQu("UPDATE  `AAA` SET `password`='{$password}' WHERE email='{$email}';");//sql语句
67                     $rows2=mysqlJi::mysqlRows();//受影响的行数
68                     if($rows==1){
69                         $cues =1;//成功了                            
70                         $b=$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); 
71                     }else{
72                         $cues = '失败';
73                     }
74                 }else{
75                     $cues = $email.'邮箱没有被注册';
76                 }
77                         
78             }else{
79                 $cues='邮箱格式不正确';
80             }        
81         }
82 
83 
84 
85  //$rli=mysqlJi::mysqlFeAs($res);//遍历结果
86  //mysqlJi::jsonEn($rli);//输出json
87  mysqlJi::mysqlCl();//关闭数据库
88 $b='[{"tishi":"'.$cues.'"}]';//输出提示
89 echo $b;
90 
91 ?>

发送邮件代码

email.class.php

  1 <?php
  2 header("Content-type: text/html; charset=utf-8");//设置utf8
  3 class smtp 
  4 { 
  5     /* Public Variables */ 
  6     public $smtp_port; 
  7     public $time_out; 
  8     public $host_name; 
  9     public $log_file; 
 10     public $relay_host; 
 11     public $debug; 
 12     public $auth; 
 13     public $user; 
 14     public $pass; 
 15      
 16     /* Private Variables */ 
 17     private $sock; 
 18  
 19     /* Constractor */ 
 20     function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) 
 21     { 
 22         $this->debug = FALSE; 
 23         $this->smtp_port = $smtp_port; 
 24         $this->relay_host = $relay_host; 
 25         $this->time_out = 30; //is used in fsockopen() 
 26         # 
 27         $this->auth = $auth;//auth 
 28         $this->user = $user; 
 29         $this->pass = $pass; 
 30         # 
 31         $this->host_name = "localhost"; //is used in HELO command 
 32         $this->log_file =""; 
 33          
 34         $this->sock = FALSE; 
 35     } 
 36  
 37     /* Main Function */ 
 38     function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") 
 39     { 
 40         $mail_from = $this->get_address($this->strip_comment($from)); 
 41         $body = preg_replace("/(^|(\r\n))(\\.)/", "\\1.\\3", $body); 
 42         $header .= "MIME-Version:1.0\r\n"; 
 43         if($mailtype=="HTML"){ 
 44             $header .= "Content-Type:text/html\r\n"; 
 45         } 
 46             $header .= "To: ".$to."\r\n"; 
 47         if ($cc != "") { 
 48             $header .= "Cc: ".$cc."\r\n"; 
 49         } 
 50         $header .= "From: $from<".$from.">\r\n"; 
 51         $header .= "Subject: ".$subject."\r\n"; 
 52         $header .= $additional_headers; 
 53         $header .= "Date: ".date("r")."\r\n"; 
 54         $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n"; 
 55         list($msec, $sec) = explode(" ", microtime()); 
 56         $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n"; 
 57         $TO = explode(",", $this->strip_comment($to)); 
 58      
 59         if ($cc != "") { 
 60         $TO = array_merge($TO, explode(",", $this->strip_comment($cc))); 
 61         } 
 62          
 63         if ($bcc != "") { 
 64         $TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); 
 65         } 
 66      
 67         $sent = TRUE; 
 68         foreach ($TO as $rcpt_to) { 
 69             $rcpt_to = $this->get_address($rcpt_to); 
 70             if (!$this->smtp_sockopen($rcpt_to)) { 
 71         //        $this->log_write("Error: Cannot send email to ".$rcpt_to."\n"); 
 72                 $sent = FALSE; 
 73                 continue; 
 74             } 
 75             if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { 
 76         //        $this->log_write("E-mail has been sent to <".$rcpt_to.">\n"); 
 77             } else { 
 78         //        $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n"); 
 79                 $sent = FALSE; 
 80             } 
 81             fclose($this->sock); 
 82           //  $this->log_write("Disconnected from remote host\n"); 
 83         } 
 84         echo "<br>"; 
 85         echo $header; 
 86         return $sent; 
 87     } 
 88  
 89     /* Private Functions */ 
 90      
 91     function smtp_send($helo, $from, $to, $header, $body = "") 
 92     { 
 93         if (!$this->smtp_putcmd("HELO", $helo)) { 
 94             return $this->smtp_error("sending HELO command"); 
 95         } 
 96         #auth 
 97         if($this->auth){ 
 98             if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { 
 99          //       return $this->smtp_error("sending HELO command"); 
100             } 
101             if (!$this->smtp_putcmd("", base64_encode($this->pass))) { 
102           //      return $this->smtp_error("sending HELO command"); 
103             } 
104         } 
105         # 
106         if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) { 
107         //    return $this->smtp_error("sending MAIL FROM command"); 
108         } 
109          
110         if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) { 
111           //  return $this->smtp_error("sending RCPT TO command"); 
112         } 
113  
114         if (!$this->smtp_putcmd("DATA")) { 
115            // return $this->smtp_error("sending DATA command"); 
116         } 
117          
118         if (!$this->smtp_message($header, $body)) { 
119           //  return $this->smtp_error("sending message"); 
120         } 
121  
122         if (!$this->smtp_eom()) { 
123           //  return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); 
124         } 
125          
126         if (!$this->smtp_putcmd("QUIT")) { 
127           //  return $this->smtp_error("sending QUIT command"); 
128         } 
129  
130         return TRUE; 
131     } 
132  
133     function smtp_sockopen($address) 
134     { 
135         if ($this->relay_host == "") { 
136             return $this->smtp_sockopen_mx($address); 
137         } else { 
138             return $this->smtp_sockopen_relay(); 
139         } 
140     } 
141  
142     function smtp_sockopen_relay() 
143     { 
144       //  $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n"); 
145         $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); 
146         if (!($this->sock && $this->smtp_ok())) { 
147          //   $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n"); 
148          //   $this->log_write("Error: ".$errstr." (".$errno.")\n"); 
149             return FALSE; 
150         } 
151    //     $this->log_write("Connected to relay host ".$this->relay_host."\n"); 
152         return TRUE;; 
153     } 
154  
155     function smtp_sockopen_mx($address) 
156     { 
157         $domain = preg_replace("/^.+@([^@]+)$/", "\\1", $address); 
158         if (!@getmxrr($domain, $MXHOSTS)) { 
159           //  $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n"); 
160             return FALSE; 
161         } 
162         foreach ($MXHOSTS as $host) { 
163        //     $this->log_write("Trying to ".$host.":".$this->smtp_port."\n"); 
164             $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); 
165             if (!($this->sock && $this->smtp_ok())) { 
166         //        $this->log_write("Warning: Cannot connect to mx host ".$host."\n"); 
167          //       $this->log_write("Error: ".$errstr." (".$errno.")\n"); 
168                 continue; 
169             } 
170          //   $this->log_write("Connected to mx host ".$host."\n"); 
171             return TRUE; 
172         } 
173      //   $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n"); 
174         return FALSE; 
175     } 
176  
177     function smtp_message($header, $body) 
178     { 
179         fputs($this->sock, $header."\r\n".$body); 
180         $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> ")); 
181          
182         return TRUE; 
183     } 
184  
185     function smtp_eom() 
186     { 
187         fputs($this->sock, "\r\n.\r\n"); 
188         $this->smtp_debug(". [EOM]\n"); 
189          
190         return $this->smtp_ok(); 
191     } 
192  
193     function smtp_ok() 
194     { 
195         $response = str_replace("\r\n", "", fgets($this->sock, 512)); 
196         $this->smtp_debug($response."\n"); 
197          
198         if (!preg_match("/^[23]/", $response)) { 
199             fputs($this->sock, "QUIT\r\n"); 
200             fgets($this->sock, 512); 
201           //  $this->log_write("Error: Remote host returned \"".$response."\"\n"); 
202             return FALSE; 
203         } 
204         return TRUE; 
205     } 

                      

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php创建简单的RestfulWebAPI(三)发布时间:2022-07-10
下一篇:
接上篇,php生成静态页面,加上页面时间缓存发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap