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 = "vip@bjxxw.com";//SMTP服务器的用户邮箱 46 $smtpemailto = $email;//发送给谁 47 $smtpuser = "vip@bjxxw.com";//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"> </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:vip@bjxxw.com" target="_blank" style="color:#2af">vip@bjxxw.com</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 } 206 207 function smtp_putcmd($cmd, $arg = "") 208 { 209 if ($arg != "") { 210 if($cmd=="") $cmd = $arg; 211 else $cmd = $cmd." ".$arg; 212 } 213 214 fputs($this->sock, $cmd."\r\n"); 215 $this->smtp_debug("> ".$cmd."\n"); 216 217 return $this->smtp_ok(); 218 } 219 220 function smtp_error($string) 221 { 222 //@ $this->log_write("Error: Error occurred while ".$string.".\n"); 223 return FALSE; 224 } 225 226 function log_write($message) 227 { 228 $this->smtp_debug($message); 229 230 if ($this->log_file == "") { 231 return TRUE; 232 } 233 234 $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message; 235 if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) { 236 $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n"); 237 return FALSE; 238 } 239 flock($fp, LOCK_EX); 240 fputs($fp, $message); 241 fclose($fp); 242 243 return TRUE; 244 } 245 246 function strip_comment($address) 247 { 248 $comment = "/\\([^()]*\\)/"; 249 while (preg_match($comment, $address)) { 250 $address = preg_replace($comment, "", $address); 251 } 252 253 return $address; 254 } 255 256 function get_address($address) 257 { 258 $address = preg_replace("/([ \t\r\n])+/", "", $address); 259 $address = preg_replace("/^.*<(.+)>.*$/", "\\1", $address); 260 261 return $address; 262 } 263 264 function smtp_debug($message) 265 { 266 if ($this->debug) { 267 // echo $message."<br>"; 268 } 269 } 270 271 function get_attach_type($image_tag) { // 272 273 $filedata = array(); 274 275 $img_file_con=fopen($image_tag,"r"); 276 unset($image_data); 277 while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag)))) 278 $image_data.=$tem_buffer; 279 fclose($img_file_con); 280 281 $filedata['context'] = $image_data; 282 $filedata['filename']= basename($image_tag); 283 $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,".")); 284 switch($extension){ 285 case ".gif": 286 $filedata['type'] = "image/gif"; 287 break; 288 case ".gz": 289 $filedata['type'] = "application/x-gzip"; 290 break; 291 case ".htm": 292 $filedata['type'] = "text/html"; 293 break; 294 case ".html": 295 $filedata['type'] = "text/html"; 296 break; 297 case ".jpg": 298 $filedata['type'] = "image/jpeg"; 299 break; 300 case ".tar": 301 $filedata['type'] = "application/x-tar"; 302 break; 303 case ".txt": 304 $filedata['type'] = "text/plain"; 305 break; 306 case ".zip": 307 $filedata['type'] = "application/zip"; 308 break; 309 default: 310 $filedata['type'] = "application/octet-stream"; 311 break; 312 } 313 return $filedata; 314 } 315 316 } // end class 317 ?>