VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php完整邮件发送类

  1. class smtp 
  2. { 
  3. /* Public Variables */ 
  4. var $smtp_port; 
  5. var $time_out; 
  6. var $host_name; 
  7. var $log_file; 
  8. var $relay_host; 
  9. var $debug; 
  10. var $auth; 
  11. var $user; 
  12. var $pass; 
  13.  
  14. /* Private Variables */  
  15. var $sock; 
  16.  
  17. /* Constractor */ 
  18. function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) 
  19. { 
  20.  $this->debug = FALSE; 
  21.  $this->smtp_port = $smtp_port; 
  22.  $this->relay_host = $relay_host; 
  23.  $this->time_out = 30; //is used in fsockopen()  
  24.  $this->auth = $auth;//auth 
  25.  $this->user = $user; 
  26.  $this->pass = $pass; 
  27.  $this->host_name = "localhost"; //is used in HELO command  
  28.  $this->log_file = ""; 
  29.  $this->sock = FALSE; 
  30. } 
  31.  
  32. /* Main Function */ 
  33. function sendmail($to, $from ,$fromname, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = ""){ 
  34.  $mail_from = $this->get_address($this->strip_comment($from)); 
  35.  $body = ereg_replace("(^|( ))(.)", ".", $body); 
  36.  $header .= "MIME-Version:1.0 "; 
  37.  if($mailtype=="HTML"){ 
  38.   $header .= "Content-Type:text/html;charset=utf-8 "; 
  39.  } 
  40.  $header .= "To: ".$to." "; 
  41.  if ($cc != ""){ 
  42.   $header .= "Cc: ".$cc." "; 
  43.  } 
  44.  $header .= "From: $fromname<".$fromname."> "; 
  45.  $header .= "Subject: ".$subject." "; 
  46.  $header .= $additional_headers; 
  47.  $header .= "Date: ".date("r")." "; 
  48.  $header .= "X-Mailer:By Redhat (PHP/".php教程version().") "; 
  49.  list($msec, $sec) = explode(" ", microtime()); 
  50.  $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from."> "; 
  51.  $TO = explode(",", $this->strip_comment($to)); 
  52.  
  53.  if ($cc != ""){ 
  54.   $TO = array_merge($TO, explode(",", $this->strip_comment($cc))); 
  55.  } 
  56.  if ($bcc != ""){ 
  57.   $TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); 
  58.  } 
  59.  $sent = TRUE; 
  60.  foreach ($TO as $rcpt_to){ 
  61.   $rcpt_to = $this->get_address($rcpt_to); 
  62.   if (!$this->smtp_sockopen($rcpt_to)){ 
  63.    $this->log_write("Error: Cannot send email to ".$rcpt_to." "); 
  64.    $sent = FALSE; 
  65.    continue; 
  66.   } 
  67.   if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)){ 
  68.    $this->log_write("E-mail has been sent to <".$rcpt_to."> "); 
  69.   }else{ 
  70.    $this->log_write("Error: Cannot send email to <".$rcpt_to."> "); 
  71.    $sent = FALSE; 
  72.   } 
  73.   fclose($this->sock); 
  74.   $this->log_write("Disconnected from remote host "); 
  75.  } 
  76.  return $sent; 
  77. } 
  78.  
  79.  /* Private Functions */ 
  80.  function smtp_send($helo, $from, $to, $header, $body = ""){ 
  81.   if (!$this->smtp_putcmd("HELO", $helo)){ 
  82.    return $this->smtp_error("sending HELO command"); 
  83.   } 
  84.    
  85.   #auth 
  86.   if($this->auth){ 
  87.    if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))){ 
  88.     return $this->smtp_error("sending HELO command"); 
  89.    } 
  90.    if (!$this->smtp_putcmd("", base64_encode($this->pass))){ 
  91.     return $this->smtp_error("sending HELO command"); 
  92.    } 
  93.   } 
  94.   if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")){ 
  95.    return $this->smtp_error("sending MAIL FROM command"); 
  96.   } 
  97.   if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")){ 
  98.    return $this->smtp_error("sending RCPT TO command"); 
  99.   } 
  100.   if (!$this->smtp_putcmd("DATA")){ 
  101.    return $this->smtp_error("sending DATA command"); 
  102.   } 
  103.   if (!$this->smtp_message($header, $body)){ 
  104.    return $this->smtp_error("sending message"); 
  105.   } 
  106.   if (!$this->smtp_eom()){ 
  107.    return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); 
  108.   } 
  109.   if (!$this->smtp_putcmd("QUIT")){ 
  110.    return $this->smtp_error("sending QUIT command"); 
  111.   } 
  112.   return TRUE; 
  113.  } 
  114.  
  115.  function smtp_sockopen($address){ 
  116.   if ($this->relay_host == ""){ 
  117.    return $this->smtp_sockopen_mx($address); 
  118.   }  
  119.   else{ 
  120.    return $this->smtp_sockopen_relay(); 
  121.   } 
  122.  } 
  123.  
  124.  function smtp_sockopen_relay(){ 
  125.   $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port." "); 
  126.   $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); 
  127.   if (!($this->sock && $this->smtp_ok())){ 
  128.    $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host." "); 
  129.    $this->log_write("Error: ".$errstr." (".$errno.") "); 
  130.    return FALSE; 
  131.   } 
  132.   $this->log_write("Connected to relay host ".$this->relay_host." "); 
  133.   return TRUE;; 
  134.  } 
  135.   
  136.  function smtp_sockopen_mx($address){ 
  137.   $domain = ereg_replace("^.+@([^@]+)$", "", $address); 
  138.   if (!@getmxrr($domain, $MXHOSTS)){ 
  139.    $this->log_write("Error: Cannot resolve MX "".$domain."" "); 
  140.    return FALSE; 
  141.   } 
  142.   foreach ($MXHOSTS as $host){ 
  143.    $this->log_write("Trying to ".$host.":".$this->smtp_port." "); 
  144.    $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); 
  145.    if (!($this->sock && $this->smtp_ok())){ 
  146.     $this->log_write("Warning: Cannot connect to mx host ".$host." "); 
  147.     $this->log_write("Error: ".$errstr." (".$errno.") "); 
  148.     continue; 
  149.    } 
  150.    $this->log_write("Connected to mx host ".$host." "); 
  151.    return TRUE; 
  152.   } 
  153.   $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).") "); 
  154.   return FALSE; 
  155.  } 
  156.  
  157.  function smtp_message($header, $body){ 
  158.   fputs($this->sock, $header." ".$body); 
  159.   $this->smtp_debug("> ".str_replace(" ", " "."> ", $header." > ".$body." > ")); 
  160.   return TRUE; 
  161.  } 
  162.   
  163.  function smtp_eom(){ 
  164.   fputs($this->sock, " . "); 
  165.   $this->smtp_debug(". [EOM] "); 
  166.   return $this->smtp_ok(); 
  167.  } 
  168.  
  169.  function smtp_ok(){ 
  170.   $response = str_replace(" ", "", fgets($this->sock, 512)); 
  171.   $this->smtp_debug($response." "); 
  172.   if (!ereg("^[23]", $response)){ 
  173.    fputs($this->sock, "QUIT "); 
  174.    fgets($this->sock, 512); 
  175.    $this->log_write("Error: Remote host returned "".$response."" "); 
  176.    return FALSE; 
  177.   } 
  178.   return TRUE; 
  179.  } 
  180.  
  181.  function smtp_putcmd($cmd, $arg = ""){ 
  182.   if($arg != ""){ 
  183.    if($cmd==""){ 
  184.     $cmd = $arg; 
  185.    } 
  186.    else{ 
  187.     $cmd = $cmd." ".$arg; 
  188.    } 
  189.   } 
  190.   fputs($this->sock, $cmd." "); 
  191.   $this->smtp_debug("> ".$cmd." "); 
  192.   return $this->smtp_ok(); 
  193.  } 
  194.  
  195.  function smtp_error($string){ 
  196.   $this->log_write("Error: Error occurred while ".$string.". "); 
  197.   return FALSE; 
  198.  } 
  199.  
  200.  function log_write($message){ 
  201.   $this->smtp_debug($message); 
  202.   if ($this->log_file == ""){ 
  203.    return TRUE; 
  204.   } 
  205.   $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message; 
  206.   if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))){ 
  207.    $this->smtp_debug("Warning: Cannot open log file "".$this->log_file."" "); 
  208.    return FALSE;; 
  209.   } 
  210.   flock($fp, LOCK_EX); 
  211.   fputs($fp, $message); 
  212.   fclose($fp); 
  213.   return TRUE; 
  214.  } 
  215.  
  216.  function strip_comment($address){//去除"()" 
  217.   $comment = "([^()]*)"; 
  218.   while (ereg($comment, $address)){ 
  219.    $address = ereg_replace($comment, "", $address); 
  220.   } 
  221.   return $address; 
  222.  } 
  223.  
  224.  function get_address($address){ 
  225.   $address = ereg_replace("([ ])+", "", $address);// 制表符 回车符   换行符  +匹配前面的子表达式一次或多次 
  226.   $address = ereg_replace("^.*<(.+)>.*$", "", $address); 
  227.   return $address;//开源代码phpfensi.com 
  228.  } 
  229.  
  230.  function smtp_debug($message){ 
  231.   if ($this->debug){ 
  232.    echo $message; 
  233.   } 
  234.  } 
  235. } 
  236.  

出处:http://www.phpfensi.com/php/20140909/5177.html


相关教程