首页 > 开发 > PHP > 正文

一个简单的PHP邮件发送类

2024-05-04 21:52:58
字体:大 中 小
来源:转载
供稿:网友
需要用PHP发送邮件,不想要phpmailer那么复杂,找来找去,总算找到了一个好用的,分享出来,代码如下.
PHP邮件发送类,一共包含两个文件.
sent_mail.php
  1. <?php  
  2. require_once ('email.class.php'); 
  3. //########################################## 
  4. $smtpserver = "smtp.163.com";//SMTP服务器 
  5. $smtpserverport =25;//SMTP服务器端口 
  6. $smtpusermail = "xxx@163.com";//SMTP服务器的用户邮箱 
  7. $smtpemailto = "xxx@qq.com";//发送给谁 
  8. $smtpuser = "xxx@looklo.com";//SMTP服务器的用户帐号 
  9. $smtppass = "*****";//SMTP服务器的用户密码 
  10. $mailsubject = "PHP100测试邮件系统";//邮件主题 
  11. $mailbody = "<h1> 这是一个测试程序 PHP100.com </h1>";//邮件内容 
  12. $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件 
  13. ########################################## 
  14. $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. 
  15. $smtp->debug = true;//是否显示发送的调试信息 
  16. $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); 
  17. ?> 
email.class.php
  1. <? 
  2. class smtp 
  3. { 
  4.     /* Public Variables */ 
  5.     public $smtp_port; 
  6.     public $time_out; 
  7.     public $host_name; 
  8.     public $log_file; 
  9.     public $relay_host; 
  10.     public $debug; 
  11.     public $auth; 
  12.     public $user; 
  13.     public $pass; 
  14.      
  15.     /* Private Variables */ 
  16.     private $sock; 
  17.  
  18.     /* Constractor */ 
  19.     function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) 
  20.     { 
  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.         $this->auth = $auth;//auth 
  27.         $this->user = $user; 
  28.         $this->pass = $pass; 
  29.         # 
  30.         $this->host_name = "localhost"; //is used in HELO command 
  31.         $this->log_file =""; 
  32.          
  33.         $this->sock = FALSE; 
  34.     } 
  35.  
  36.     /* Main Function */ 
  37.     function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") 
  38.     { 
  39.         $mail_from = $this->get_address($this->strip_comment($from)); 
  40.         $body = preg_replace("/(^|(/r/n))(//.)/", "//1.//3", $body); 
  41.         $header .= "MIME-Version:1.0/r/n"; 
  42.         if($mailtype=="HTML"){ 
  43.             $header .= "Content-Type:text/html/r/n"; 
  44.         } 
  45.             $header .= "To: ".$to."/r/n"; 
  46.         if ($cc != "") { 
  47.             $header .= "Cc: ".$cc."/r/n"; 
  48.         } 
  49.         $header .= "From: $from<".$from.">/r/n"; 
  50.         $header .= "Subject: ".$subject."/r/n"; 
  51.         $header .= $additional_headers; 
  52.         $header .= "Date: ".date("r")."/r/n"; 
  53.         $header .= "X-Mailer:By Redhat (PHP/".phpversion().")/r/n"; 
  54.         list($msec, $sec) = explode(" ", microtime()); 
  55.         $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">/r/n"; 
  56.         $TO = explode(",", $this->strip_comment($to)); 
  57.      
  58.         if ($cc != "") { 
  59.         $TO = array_merge($TO, explode(",", $this->strip_comment($cc))); 
  60.         } 
  61.          
  62.         if ($bcc != "") { 
  63.         $TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); 
  64.         } 
  65.      
  66.         $sent = TRUE; 
  67.         foreach ($TO as $rcpt_to) { 
  68.             $rcpt_to = $this->get_address($rcpt_to); 
  69.             if (!$this->smtp_sockopen($rcpt_to)) { 
  70.                 $this->log_write("Error: Cannot send email to ".$rcpt_to."/n"); 
  71.                 $sent = FALSE; 
  72.                 continue; 
  73.             } 
  74.             if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { 
  75.                 $this->log_write("E-mail has been sent to <".$rcpt_to.">/n"); 
  76.             } else { 
  77.                 $this->log_write("Error: Cannot send email to <".$rcpt_to.">/n"); 
  78.                 $sent = FALSE; 
  79.             } 
  80.             fclose($this->sock); 
  81.             $this->log_write("Disconnected from remote host/n"); 
  82.         } 
  83.         echo "<br>"; 
  84.         echo $header; 
  85.         return $sent; 
  86.     } 
  87.  
  88.     /* Private Functions */ 
  89.      
  90.     function smtp_send($helo, $from, $to, $header, $body = "") 
  91.     { 
  92.         if (!$this->smtp_putcmd("HELO", $helo)) { 
  93.             return $this->smtp_error("sending HELO command"); 
  94.         } 
  95.         #auth 
  96.         if($this->auth){ 
  97.             if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { 
  98.                 return $this->smtp_error("sending HELO command"); 
  99.             } 
  100.             if (!$this->smtp_putcmd("", base64_encode($this->pass))) { 
  101.                 return $this->smtp_error("sending HELO command"); 
  102.             } 
  103.         } 
  104.         # 
  105.         if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) { 
  106.             return $this->smtp_error("sending MAIL FROM command"); 
  107.         } 
  108.          
  109.         if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) { 
  110.             return $this->smtp_error("sending RCPT TO command"); 
  111.         } 
  112.  
  113.         if (!$this->smtp_putcmd("DATA")) { 
  114.             return $this->smtp_error("sending DATA command"); 
  115.         } 
  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.  
  129.         return TRUE; 
  130.     } 
  131.  
  132.     function smtp_sockopen($address) 
  133.     { 
  134.         if ($this->relay_host == "") { 
  135.             return $this->smtp_sockopen_mx($address); 
  136.         } else { 
  137.             return $this->smtp_sockopen_relay(); 
  138.         } 
  139.     } 
  140.  
  141.     function smtp_sockopen_relay() 
  142.     { 
  143.         $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."/n"); 
  144.         $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); 
  145.         if (!($this->sock && $this->smtp_ok())) { 
  146.             $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."/n"); 
  147.             $this->log_write("Error: ".$errstr." (".$errno.")/n"); 
  148.             return FALSE; 
  149.         } 
  150.         $this->log_write("Connected to relay host ".$this->relay_host."/n"); 
  151.         return TRUE;; 
  152.     } 
  153.  
  154.     function smtp_sockopen_mx($address) 
  155.     { 
  156.         $domain = preg_replace("/^.+@([^@]+)$/", "//1", $address); 
  157.         if (!@getmxrr($domain, $MXHOSTS)) { 
  158.             $this->log_write("Error: Cannot resolve MX /"".$domain."/"/n"); 
  159.             return FALSE; 
  160.         } 
  161.         foreach ($MXHOSTS as $host) { 
  162.             $this->log_write("Trying to ".$host.":".$this->smtp_port."/n"); 
  163.             $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); 
  164.             if (!($this->sock && $this->smtp_ok())) { 
  165.                 $this->log_write("Warning: Cannot connect to mx host ".$host."/n"); 
  166.                 $this->log_write("Error: ".$errstr." (".$errno.")/n"); 
  167.                 continue; 
  168.             } 
  169.             $this->log_write("Connected to mx host ".$host."/n"); 
  170.             return TRUE; 
  171.         } 
  172.         $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")/n"); 
  173.         return FALSE; 
  174.     } 
  175.  
  176.     function smtp_message($header, $body) 
  177.     { 
  178.         fputs($this->sock, $header."/r/n".$body); 
  179.         $this->smtp_debug("> ".str_replace("/r/n", "/n"."> ", $header."/n> ".$body."/n> ")); 
  180.          
  181.         return TRUE; 
  182.     } 
  183.  
  184.     function smtp_eom() 
  185.     { 
  186.         fputs($this->sock, "/r/n./r/n"); 
  187.         $this->smtp_debug(". [EOM]/n"); 
  188.          
  189.         return $this->smtp_ok(); 
  190.     } 
  191.  
  192.     function smtp_ok() 
  193.     { 
  194.         $response = str_replace("/r/n", "", fgets($this->sock, 512)); 
  195.         $this->smtp_debug($response."/n"); 
  196.          
  197.         if (!preg_match("/^[23]/", $response)) { 
  198.             fputs($this->sock, "QUIT/r/n"); 
  199.             fgets($this->sock, 512); 
  200.             $this->log_write("Error: Remote host returned /"".$response."/"/n"); 
  201.             return FALSE; 
  202.         } 
  203.         return TRUE; 
  204.     } 
  205.  
  206.     function smtp_putcmd($cmd, $arg = "") 
  207.     { 
  208.         if ($arg != "") { 
  209.             if($cmd=="") $cmd = $arg; 
  210.             else $cmd = $cmd." ".$arg; 
  211.         } 
  212.  
  213.         fputs($this->sock, $cmd."/r/n"); 
  214.         $this->smtp_debug("> ".$cmd."/n"); 
  215.  
  216.         return $this->smtp_ok(); 
  217.     } 
  218.  
  219.     function smtp_error($string) 
  220.     { 
  221.         $this->log_write("Error: Error occurred while ".$string."./n"); 
  222.         return FALSE; 
  223.     } 
  224.  
  225.     function log_write($message) 
  226.     { 
  227.         $this->smtp_debug($message); 
  228.          
  229.         if ($this->log_file == "") { 
  230.             return TRUE; 
  231.         } 
  232.  
  233.         $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message; 
  234.         if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) { 
  235.             $this->smtp_debug("Warning: Cannot open log file /"".$this->log_file."/"/n"); 
  236.             return FALSE; 
  237.         } 
  238.         flock($fp, LOCK_EX); 
  239.         fputs($fp, $message); 
  240.         fclose($fp); 
  241.          
  242.         return TRUE; 
  243.     } 
  244.  
  245.     function strip_comment($address) 
  246.     { 
  247.         $comment = "///([^()]*//)/"; 
  248.         while (preg_match($comment, $address)) { 
  249.             $address = preg_replace($comment, "", $address); 
  250.         } 
  251.          
  252.         return $address; 
  253.     } 
  254.  
  255.     function get_address($address) 
  256.     { 
  257.         $address = preg_replace("/([ /t/r/n])+/", "", $address); 
  258.         $address = preg_replace("/^.*<(.+)>.*$/", "//1", $address); 
  259.          
  260.         return $address; 
  261.     } 
  262.  
  263.     function smtp_debug($message) 
  264.     { 
  265.         if ($this->debug) { 
  266.             echo $message."<br>"; 
  267.         } 
  268.     } 
  269.  
  270. function get_attach_type($image_tag) { // 
  271.  
  272.     $filedata = array(); 
  273.      
  274.     $img_file_con=fopen($image_tag,"r"); 
  275.     unset($image_data); 
  276.     while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag)))) 
  277.     $image_data.=$tem_buffer; 
  278.     fclose($img_file_con); 
  279.  
  280.     $filedata['context'] = $image_data; 
  281.     $filedata['filename']= basename($image_tag); 
  282.     $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,".")); 
  283.     switch($extension){ 
  284.         case ".gif": 
  285.             $filedata['type'] = "image/gif"; 
  286.             break; 
  287.         case ".gz": 
  288.             $filedata['type'] = "application/x-gzip"; 
  289.             break; 
  290.         case ".htm": 
  291.             $filedata['type'] = "text/html"; 
  292.             break; 
  293.         case ".html": 
  294.             $filedata['type'] = "text/html"; 
  295.             break; 
  296.         case ".jpg": 
  297.             $filedata['type'] = "image/jpeg"; 
  298.             break; 
  299.         case ".tar": 
  300.             $filedata['type'] = "application/x-tar"; 
  301.             break; 
  302.         case ".txt": 
  303.             $filedata['type'] = "text/plain"; 
  304.             break; 
  305.         case ".zip": 
  306.             $filedata['type'] = "application/zip"; 
  307.             break; 
  308.         default: 
  309.             $filedata['type'] = "application/octet-stream"; 
  310.             break; 
  311.     } 
  312.     return $filedata; 
  313.     } 
  314.  
  315. } // end class 
  316. ?> 
武林网原创,转载请保留出处.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表