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

最模板

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