自动发邮件 使用了这个类http://bbs.php100.com/read-htm-tid-121431.html
因他用的php版本较老,用到了函数ereg_replace() 和 ereg()
解决方法
1、对于ereg_replace() 函数,替换为 strtr() 函数
语法不同点如下:
详情http://www.php.net/manual/zh/function.ereg-replace.php
详情http://www.w3school.com.cn/php/func_string_strtr.asp
2、对于ereg() 函数,替换为 preg_match() 函数
链接http://blog.163.com/yanli_xu/blog/static/1363672912011420101820393/
preg_match() 函数语法http://www.php.net/manual/zh/function.preg-match.php
修改完毕后,完整代码如下
【邮件类】sm.class.php
1 <?php 2 class smtp 3 4 { 5 6 /* Public Variables */ 7 8 var $smtp_port; 9 10 var $time_out; 11 12 var $host_name; 13 14 var $log_file; 15 16 var $relay_host; 17 18 var $debug; 19 20 var $auth; 21 22 var $user; 23 24 var $pass; 25 26 /* Private Variables */ 27 var $sock; 28 29 /* Constractor */ 30 31 function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) 32 33 { 34 35 $this->debug = FALSE; 36 37 $this->smtp_port = $smtp_port; 38 39 $this->relay_host = $relay_host; 40 41 $this->time_out = 30; //is used in fsockopen() 42 # 43 44 $this->auth = $auth;//auth 45 46 $this->user = $user; 47 48 $this->pass = $pass; 49 50 # 51 52 $this->host_name = "localhost"; //is used in HELO command 53 $this->log_file = ""; 54 55 56 57 $this->sock = FALSE; 58 59 } 60 61 /* Main Function */ 62 63 function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") 64 65 { 66 67 $mail_from = $this->get_address($this->strip_comment($from)); 68 69 //$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body); 70 $body = strtr($body, "(^|(\r\n))(\.)", "\1.\3"); 71 $header .= "MIME-Version:1.0\r\n"; 72 73 if($mailtype=="HTML"){ 74 75 $header .= "Content-Type:text/html\r\n"; 76 77 } 78 79 $header .= "To: ".$to."\r\n"; 80 81 if ($cc != "") { 82 83 $header .= "Cc: ".$cc."\r\n"; 84 85 } 86 87 $header .= "From: $from<".$from.">\r\n"; 88 89 $header .= "Subject: ".$subject."\r\n"; 90 91 $header .= $additional_headers; 92 93 $header .= "Date: ".date("r")."\r\n"; 94 95 $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n"; 96 97 list($msec, $sec) = explode(" ", microtime()); 98 99 $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n"; 100 101 $TO = explode(",", $this->strip_comment($to)); 102 103 if ($cc != "") { 104 105 $TO = array_merge($TO, explode(",", $this->strip_comment($cc))); 106 107 } 108 109 if ($bcc != "") { 110 111 $TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); 112 113 } 114 115 $sent = TRUE; 116 117 foreach ($TO as $rcpt_to) { 118 119 $rcpt_to = $this->get_address($rcpt_to); 120 121 if (!$this->smtp_sockopen($rcpt_to)) { 122 123 $this->log_write("Error: Cannot send email to ".$rcpt_to."\n"); 124 125 $sent = FALSE; 126 127 continue; 128 129 } 130 131 if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { 132 133 $this->log_write("E-mail has been sent to <".$rcpt_to.">\n"); 134 135 } else { 136 137 $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n"); 138 139 $sent = FALSE; 140 141 } 142 143 fclose($this->sock); 144 145 $this->log_write("Disconnected from remote host\n"); 146 147 } 148 149 return $sent; 150 151 } 152 153 154 155 /* Private Functions */ 156 157 158 159 function smtp_send($helo, $from, $to, $header, $body = "") 160 161 { 162 163 if (!$this->smtp_putcmd("HELO", $helo)) { 164 165 return $this->smtp_error("sending HELO command"); 166 167 } 168 169 #auth 170 171 if($this->auth){ 172 173 if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { 174 175 return $this->smtp_error("sending HELO command"); 176 177 } 178 179 if (!$this->smtp_putcmd("", base64_encode($this->pass))) { 180 181 return $this->smtp_error("sending HELO command"); 182 183 } 184 185 } 186 187 # 188 189 if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) { 190 191 return $this->smtp_error("sending MAIL FROM command"); 192 193 } 194 195 if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) { 196 197 return $this->smtp_error("sending RCPT TO command"); 198 199 } 200 201 if (!$this->smtp_putcmd("DATA")) { 202 203 return $this->smtp_error("sending DATA command"); 204 205 } 206 207 if (!$this->smtp_message($header, $body)) { 208 209 return $this->smtp_error("sending message"); 210 211 } 212 213 if (!$this->smtp_eom()) { 214 215 return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); 216 217 } 218 219 if (!$this->smtp_putcmd("QUIT")) { 220 221 return $this->smtp_error("sending QUIT command"); 222 223 } 224 225 return TRUE; 226 227 } 228 229 function smtp_sockopen($address) 230 231 { 232 233 if ($this->relay_host == "") { 234 235 return $this->smtp_sockopen_mx($address); 236 237 } else { 238 239 return $this->smtp_sockopen_relay(); 240 241 } 242 243 } 244 245 function smtp_sockopen_relay() 246 247 { 248 249 $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n"); 250 251 $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); 252 253 if (!($this->sock && $this->smtp_ok())) { 254 255 $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n"); 256 257 $this->log_write("Error: ".$errstr." (".$errno.")\n"); 258 259 return FALSE; 260 261 } 262 263 $this->log_write("Connected to relay host ".$this->relay_host."\n"); 264 265 return TRUE;; 266 267 } 268 269 270 271 function smtp_sockopen_mx($address) 272 273 { 274 275 $domain = strtr($address, "^.+@([^@]+)$", "\1"); 276 277 if (!@getmxrr($domain, $MXHOSTS)) { 278 279 $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n"); 280 281 return FALSE; 282 283 } 284 285 foreach ($MXHOSTS as $host) { 286 287 $this->log_write("Trying to ".$host.":".$this->smtp_port."\n"); 288 289 $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); 290 291 if (!($this->sock && $this->smtp_ok())) { 292 293 $this->log_write("Warning: Cannot connect to mx host ".$host."\n"); 294 295 $this->log_write("Error: ".$errstr." (".$errno.")\n"); 296 297 continue; 298 299 } 300 301 $this->log_write("Connected to mx host ".$host."\n"); 302 303 return TRUE; 304 305 } 306 307 $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n"); 308 309 return FALSE; 310 311 } 312 313 314 315 function smtp_message($header, $body) 316 317 { 318 319 fputs($this->sock, $header."\r\n".$body); 320 321 $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> ")); 322 323 324 325 return TRUE; 326 327 } 328 329 330 331 function smtp_eom() 332 333 { 334 335 fputs($this->sock, "\r\n.\r\n"); 336 337 $this->smtp_debug(". [EOM]\n"); 338 339 340 341 return $this->smtp_ok(); 342 343 } 344 345 346 347 function smtp_ok() 348 349 { 350 351 $response = str_replace("\r\n", "", fgets($this->sock, 512)); 352 353 $this->smtp_debug($response."\n"); 354 355 356 357 if (!preg_match("/^[23]/", $response)) { 358 359 fputs($this->sock, "QUIT\r\n"); 360 361 fgets($this->sock, 512); 362 363 $this->log_write("Error: Remote host returned \"".$response."\"\n"); 364 365 return FALSE; 366 367 } 368 369 return TRUE; 370 371 } 372 373 function smtp_putcmd($cmd, $arg = "") 374 375 { 376 377 if ($arg != "") { 378 379 if($cmd=="") $cmd = $arg; 380 381 else $cmd = $cmd." ".$arg; 382 383 } 384 385 fputs($this->sock, $cmd."\r\n"); 386 387 $this->smtp_debug("> ".$cmd."\n"); 388 389 return $this->smtp_ok(); 390 391 } 392 393 function smtp_error($string) 394 395 { 396 397 $this->log_write("Error: Error occurred while ".$string.".\n"); 398 399 return FALSE; 400 401 } 402 403 function log_write($message) 404 405 { 406 407 $this->smtp_debug($message); 408 409 if ($this->log_file == "") { 410 411 return TRUE; 412 413 } 414 415 $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message; 416 417 if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) { 418 419 $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n"); 420 421 return FALSE;; 422 423 } 424 425 flock($fp, LOCK_EX); 426 427 fputs($fp, $message); 428 429 fclose($fp); 430 431 432 return TRUE; 433 434 } 435 436 437 function strip_comment($address) 438 439 { 440 441 $comment = "/\([^()]*\)/"; 442 443 while (preg_match($comment, $address)) { 444 445 $address = strtr($address, $comment, ""); 446 447 } 448 449 450 return $address; 451 452 } 453 454 455 function get_address($address) 456 457 { 458 459 $address = strtr($address, "([ \t\r\n])+", ""); 460 461 $address = strtr($address, "^.*<(.+)>.*$", "\1"); 462 463 return $address; 464 465 } 466 467 function smtp_debug($message) 468 469 { 470 471 if ($this->debug) { 472 473 echo $message; 474 475 } 476 477 } 478 479 } 480 481 ?>
【发邮件】sm.php
1 <?php 2 /* 3 这是一个测试程序!!! 4 请按照说明设置好以下的参数 5 */ 6 require("sm.class.php"); 7 ########################################## 8 $smtpserver = "smtp.qq.com";//SMTP服务器 9 $smtpserverport =25;//SMTP服务器端口 10 $smtpusermail = "你的邮箱";//SMTP服务器的用户邮箱 11 $smtpemailto = "1477299439@qq.com";//发送给谁 12 $smtpuser = "你的账号";//SMTP服务器的用户帐号(邮箱地址) 13 $smtppass = "你的密码";//SMTP服务器的用户密码(邮箱密码) 14 $mailsubject = "项目提醒";//邮件主题 15 $mailbody = "<h1>有新项目了</h1>";//邮件内容 16 $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件 17 ########################################## 18 $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. 19 $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); 20 ?>