最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

php socket 使用smtp服务器发送邮件

时间:2014-06-09 16:40来源: 作者: 点击: 次
/*邮件发送类 *功能:phpsocket使用smtp服务器发送邮件 *作者:longlong *时间:2007-11-26 */ class smtp { /*全局变量*/ var $smtp_port ; var $time_out ; var $host_name ; var $log_file ; var $relay_host ; var $debug ; var $auth ; var
  1. /*邮件发送类 
  2. *功能:php socket 使用smtp服务器发送邮件 
  3. *作者:longlong 
  4. *时间:2007-11-26 
  5. */ 
  6. class smtp 
  7. { 
  8. /* 全局变量 */ 
  9. var $smtp_port; 
  10. var $time_out; 
  11. var $host_name; 
  12. var $log_file; 
  13. var $relay_host; 
  14. var $debug; 
  15. var $auth; 
  16. var $user; 
  17. var $pass; 
  18. /* private variables */   
  19. var $sock; 
  20. /* 构造函数 */ 
  21. function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) 
  22. { 
  23.   $this->debug = false; 
  24.   $this->smtp_port = $smtp_port; 
  25.   $this->relay_host = $relay_host; 
  26.   $this->time_out = 30; //is used in fsockopen()   
  27.      
  28.   # 
  29.   $this->auth = $auth;//auth 
  30.   $this->user = $user; 
  31.   $this->pass = $pass; 
  32.      
  33.   # 
  34.   $this->host_name = "localhost"; //is used in helo command   
  35.   $this->log_file = ""; 
  36.  
  37.   $this->sock = false; 
  38. } 
  39. /* 主函数,发送邮件 */ 
  40. function sendmail($flag, $boundary, $to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") 
  41. { 
  42.   $mail_from = $this->get_address($this->strip_comment($from)); 
  43.   $body = ereg_replace("(^|(rn))(.)", "1.3", $body); 
  44.   $header = "mime-version:1.0rn"; 
  45.      
  46.   //* 
  47.   if($mailtype=="html"){ 
  48.   //echo $boundary;exit; 
  49. if($flag==2) 
  50. { 
  51.   $header .= "content-type:multipart/mixed; boundary= $boundaryrn"; 
  52.   //$header .= "content-type:text/htmlrn"; 
  53. } 
  54. else 
  55. {   
  56.   $header .= "content-type:text/htmlrn"; 
  57. } 
  58.   } 
  59.   //*/ 
  60.   $header .= "to: ".$to."rn"; 
  61.   if ($cc != "") { 
  62.   $header .= "cc: ".$cc."rn"; 
  63.   } 
  64.   $header .= "from: $from<".$from.">rn"; 
  65.   $header .= "subject: ".$subject."rn"; 
  66.   $header .= $additional_headers; 
  67.   $header .= "date: ".date("r")."rn"; 
  68.   $header .= "x-mailer:by redhat (php/".phpversion().")rn"; 
  69.   //$header.=$body;//edit by shaolong 
  70.     
  71.   list($msec, $sec) = explode(" ", microtime()); 
  72.   $header .= "message-id: <".date("ymdhis", $sec).".".($msec*1000000).".".$mail_from.">rn"; 
  73.   $to = explode(",", $this->strip_comment($to)); 
  74.   if ($cc != "") { 
  75.   $to = array_merge($to, explode(",", $this->strip_comment($cc))); 
  76.   } 
  77.  
  78.   if ($bcc != "") { 
  79.   $to = array_merge($to, explode(",", $this->strip_comment($bcc))); 
  80.   } 
  81.   $sent = true; 
  82.   foreach ($to as $rcpt_to) { 
  83.   $rcpt_to = $this->get_address($rcpt_to); 
  84.      
  85.   if (!$this->smtp_sockopen($rcpt_to)) { 
  86.   $this->log_write("error: cannot send email to ".$rcpt_to."n"); 
  87.   $sent = false; 
  88.   continue; 
  89.   } 
  90.   if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { 
  91.   $this->log_write("e-mail has been sent to <".$rcpt_to.">n"); 
  92.   } else { 
  93.   $this->log_write("error: cannot send email to <".$rcpt_to.">n"); 
  94.   $sent = false; 
  95.   } 
  96.   fclose($this->sock); 
  97.   $this->log_write("disconnected from remote hostn"); 
  98.   } 
  99.   return $sent; 
  100. } 
  101. /* 私有函数 */ 
  102. function smtp_send($helo, $from, $to, $header, $body = "") 
  103. { 
  104.   if (!$this->smtp_putcmd("helo", $helo)) { 
  105.   return $this->smtp_error("sending helo command"); 
  106.   } 
  107.   #auth 
  108.   if($this->auth){ 
  109.   if (!$this->smtp_putcmd("auth login", base64_encode($this->user))) { 
  110.   return $this->smtp_error("sending helo command"); 
  111.   } 
  112.   if (!$this->smtp_putcmd("", base64_encode($this->pass))) { 
  113.   return $this->smtp_error("sending helo command"); 
  114.   } 
  115.   } 
  116.   # 
  117.   if (!$this->smtp_putcmd("mail", "from:<".$from.">")) { 
  118.   return $this->smtp_error("sending mail from command"); 
  119.   } 
  120.   if (!$this->smtp_putcmd("rcpt", "to:<".$to.">")) { 
  121.   return $this->smtp_error("sending rcpt to command"); 
  122.   } 
  123.   if (!$this->smtp_putcmd("data")) { 
  124.   return $this->smtp_error("sending data command"); 
  125.   } 
  126.   if (!$this->smtp_message($header, $body)) { 
  127.   return $this->smtp_error("sending message"); 
  128.   } 
  129.  
  130.   if (!$this->smtp_eom()) { 
  131.   return $this->smtp_error("sending <cr><lf>.<cr><lf> [eom]"); 
  132.   } 
  133.  
  134.   if (!$this->smtp_putcmd("quit")) { 
  135.   return $this->smtp_error("sending quit command"); 
  136.   } 
  137.   return true; 
  138. } 
  139.  
  140. function smtp_sockopen($address) 
  141. { 
  142.   if ($this->relay_host == "") { 
  143.   return $this->smtp_sockopen_mx($address); 
  144.   } else { 
  145.   return $this->smtp_sockopen_relay(); 
  146.   } 
  147. } 
  148.  
  149. function smtp_sockopen_relay() 
  150. { 
  151.   $this->log_write("trying to ".$this->relay_host.":".$this->smtp_port."n"); 
  152.   $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); 
  153.   if (!($this->sock && $this->smtp_ok())) { 
  154.   $this->log_write("error: cannot connenct to relay host ".$this->relay_host."n"); 
  155.   $this->log_write("error: ".$errstr." (".$errno.")n"); 
  156.   return false; 
  157.   } 
  158.   $this->log_write("connected to relay host ".$this->relay_host."n"); 
  159.   return true; 
  160. }  
  161.  
  162. function smtp_sockopen_mx($address) 
  163. { 
  164.   $domain = ereg_replace("^.+@([^@]+)$", "1", $address); 
  165.   if (!@getmxrr($domain, $mxhosts)) { 
  166.   $this->log_write("error: cannot resolve mx "".$domain.""n"); 
  167.   return false; 
  168.   } 
  169.   foreach ($mxhosts as $host) { 
  170.   $this->log_write("trying to ".$host.":".$this->smtp_port."n"); 
  171.   $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); 
  172.   if (!($this->sock && $this->smtp_ok())) { 
  173.   $this->log_write("warning: cannot connect to mx host ".$host."n"); 
  174.   $this->log_write("error: ".$errstr." (".$errno.")n"); 
  175.   continue; 
  176.   } 
  177.   $this->log_write("connected to mx host ".$host."n"); 
  178.   return true; 
  179.   } 
  180.   $this->log_write("error: cannot connect to any mx hosts (".implode(", ", $mxhosts).")n"); 
  181.   return false; 
  182. } 
  183.  
  184. function smtp_message($header, $body) 
  185. { 
  186.   fputs($this->sock, $header."rn".$body); 
  187.   $this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> ")); 
  188.   return true; 
  189. } 
  190.  
  191. function smtp_eom() 
  192. { 
  193.   fputs($this->sock, "rn.rn"); 
  194.   $this->smtp_debug(". [eom]n"); 
  195.   return $this->smtp_ok(); 
  196. } 
  197.  
  198. function smtp_ok() 
  199. { 
  200.   $response = str_replace("rn", "", fgets($this->sock, 512)); 
  201.   $this->smtp_debug($response."n"); 
  202.   if (!ereg("^[23]", $response)) { 
  203.   fputs($this->sock, "quitrn"); 
  204.   fgets($this->sock, 512);   
  205.   $this->log_write("error: remote host returned "".$response.""n"); 
  206.   return false; 
  207.   } 
  208.   return true; 
  209. } 
  210.  
  211. function smtp_putcmd($cmd, $arg = "") 
  212. { 
  213.   if ($arg != "") { 
  214.   if($cmd=="") $cmd = $arg; 
  215.   else $cmd = $cmd." ".$arg; 
  216.   } 
  217.   fputs($this->sock, $cmd."rn"); 
  218.   $this->smtp_debug("> ".$cmd."n"); 
  219.   return $this->smtp_ok(); 
  220. } 
  221.  
  222. function smtp_error($string) 
  223. { 
  224.   $this->log_write("error: error occurred while ".$string.".n"); 
  225.   return false; 
  226. } 
  227.  
  228. function log_write($message) 
  229. { 
  230.   $this->smtp_debug($message); 
  231.   if ($this->log_file == "") { 
  232.   return true; 
  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.   return true; 
  243. } 
  244. function strip_comment($address) 
  245. { 
  246.   $comment = "([^()]*)"; 
  247.   while (ereg($comment, $address)) { 
  248.   $address = ereg_replace($comment, "", $address); 
  249.   } 
  250.   return $address; 
  251. } 
  252. function get_address($address) 
  253. { 
  254.   $address = ereg_replace("([ trn])+", "", $address); 
  255.   $address = ereg_replace("^.*<(.+)>.*$", "1", $address); 
  256.   return $address; 
  257. } 
  258.  
  259. function smtp_debug($message) 
  260. { 
  261.   if ($this->debug) { 
  262.   echo $message; 
  263.   } 
  264. } 
  265. } 
(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容