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

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

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


相关教程