一个简单的PHP邮件发送类_PHP教程

编辑Tag赚U币

推荐:PHP生成缩略图失真/不清晰的解决方法
PHP生成缩略图失真/不清晰的解决方法 1.用imagecreatetruecolor和imageCopyreSampled函数分别取代imagecreate和imagecopyresized 2.给imagejpeg的第三个参数带上100(例:imagejpeg($ni,$toFile,100)) 建议将100设置为90.否则生成图片会很大,且效果跟90没有太大区别.

需要用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.     /* Public Variables */ 
  4.     public $smtp_port
  5.     public $time_out
  6.     public $host_name
  7.     public $log_file
  8.     public $relay_host
  9.     public $debug
  10.     public $auth
  11.     public $user
  12.     public $pass
  13.      
  14.     /* Private Variables */ 
  15.     private $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.         # 
  25.         $this->auth = $auth;//auth 
  26.         $this->user = $user
  27.         $this->pass = $pass
  28.         # 
  29.         $this->host_name = "localhost"//is used in HELO command 
  30.         $this->log_file =""
  31.          
  32.         $this->sock = FALSE; 
  33.     } 
  34.  
  35.     /* Main Function */ 
  36.     function sendmail($to$from$subject = ""$body = ""$mailtype$cc = ""$bcc = ""$additional_headers = ""
  37.     { 
  38.         $mail_from = $this->get_address($this->strip_comment($from)); 
  39.         $body = preg_replace("/(^|(\r\n))(\\.)/""\\1.\\3"$body); 
  40.         $header .= "MIME-Version:1.0\r\n"
  41.         if($mailtype=="HTML"){ 
  42.             $header .= "Content-Type:text/html\r\n"
  43.         } 
  44.             $header .= "To: ".$to."\r\n"
  45.         if ($cc != "") { 
  46.             $header .= "Cc: ".$cc."\r\n"
  47.         } 
  48.         $header .= "From: $from<".$from.">\r\n"
  49.         $header .= "Subject: ".$subject."\r\n"
  50.         $header .= $additional_headers
  51.         $header .= "Date: ".date("r")."\r\n"
  52.         $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n"
  53.         list($msec$sec) = explode(" ", microtime()); 
  54.         $header .= "Message-ID: <".date("YmdHis"$sec).".".($msec*1000000).".".$mail_from.">\r\n"
  55.         $TO = explode(","$this->strip_comment($to)); 
  56.      
  57.         if ($cc != "") { 
  58.         $TO = array_merge($TOexplode(","$this->strip_comment($cc))); 
  59.         } 
  60.          
  61.         if ($bcc != "") { 
  62.         $TO = array_merge($TOexplode(","$this->strip_comment($bcc))); 
  63.         } 
  64.      
  65.         $sent = TRUE; 
  66.         foreach ($TO as $rcpt_to) { 
  67.             $rcpt_to = $this->get_address($rcpt_to); 
  68.             if (!$this->smtp_sockopen($rcpt_to)) { 
  69.                 $this->log_write("Error: Cannot send email to ".$rcpt_to."\n"); 
  70.                 $sent = FALSE; 
  71.                 continue
  72.             } 
  73.             if ($this->smtp_send($this->host_name, $mail_from$rcpt_to$header$body)) { 
  74.                 $this->log_write("E-mail has been sent to <".$rcpt_to.">\n"); 
  75.             } else { 
  76.                 $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n"); 
  77.                 $sent = FALSE; 
  78.             } 
  79.             fclose($this->sock); 
  80.             $this->log_write("Disconnected from remote host\n"); 
  81.         } 
  82.         echo "<br>"
  83.         echo $header
  84.         return $sent
  85.     } 
  86.  
  87.     /* Private Functions */ 
  88.      
  89.     function smtp_send($helo$from$to$header$body = ""
  90.     { 
  91.         if (!$this->smtp_putcmd("HELO"$helo)) { 
  92.             return $this->smtp_error("sending HELO command"); 
  93.         } 
  94.         #auth 
  95.         if($this->auth){ 
  96.             if (!$this->smtp_putcmd("AUTH LOGIN"base64_encode($this->user))) { 
  97.                 return $this->smtp_error("sending HELO command"); 
  98.             } 
  99.             if (!$this->smtp_putcmd(""base64_encode($this->pass))) { 
  100.                 return $this->smtp_error("sending HELO command"); 
  101.             } 
  102.         } 
  103.         # 
  104.         if (!$this->smtp_putcmd("MAIL""FROM:<".$from.">")) { 
  105.             return $this->smtp_error("sending MAIL FROM command"); 
  106.         } 
  107.          
  108.         if (!$this->smtp_putcmd("RCPT""TO:<".$to.">")) { 
  109.             return $this->smtp_error("sending RCPT TO command"); 
  110.         } 
  111.  
  112.         if (!$this->smtp_putcmd("DATA")) { 
  113.             return $this->smtp_error("sending DATA command"); 
  114.         } 
  115.          
  116.         if (!$this->smtp_message($header$body)) { 
  117.             return $this->smtp_error("sending message"); 
  118.         } 
  119.  
  120.         if (!$this->smtp_eom()) { 
  121.             return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); 
  122.         } 
  123.          
  124.         if (!$this->smtp_putcmd("QUIT")) { 
  125.             return $this->smtp_error("sending QUIT command"); 
  126.         } 
  127.  
  128.         return TRUE; 
  129.     } 
  130.  
  131.     function smtp_sockopen($address
  132.     { 
  133.         if ($this->relay_host == "") { 
  134.             return $this->smtp_sockopen_mx($address); 
  135.         } else { 
  136.             return $this->smtp_sockopen_relay(); 
  137.         } 
  138.     } 
  139.  
  140.     function smtp_sockopen_relay() 
  141.     { 
  142.         $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n"); 
  143.         $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno$errstr$this->time_out); 
  144.         if (!($this->sock && $this->smtp_ok())) { 
  145.             $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n"); 
  146.             $this->log_write("Error: ".$errstr." (".$errno.")\n"); 
  147.             return FALSE; 
  148.         } 
  149.         $this->log_write("Connected to relay host ".$this->relay_host."\n"); 
  150.         return TRUE;; 
  151.     } 
  152.  
  153.     function smtp_sockopen_mx($address
  154.     { 
  155.         $domain = preg_replace("/^.+@([^@]+)$/""\\1"$address); 
  156.         if (!@getmxrr($domain$MXHOSTS)) { 
  157.             $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n"); 
  158.             return FALSE; 
  159.         } 
  160.         foreach ($MXHOSTS as $host) { 
  161.             $this->log_write("Trying to ".$host.":".$this->smtp_port."\n"); 
  162.             $this->sock = @fsockopen($host$this->smtp_port, $errno$errstr$this->time_out); 
  163.             if (!($this->sock && $this->smtp_ok())) { 
  164.                 $this->log_write("Warning: Cannot connect to mx host ".$host."\n"); 
  165.                 $this->log_write("Error: ".$errstr." (".$errno.")\n"); 
  166.                 continue
  167.             } 
  168.             $this->log_write("Connected to mx host ".$host."\n"); 
  169.             return TRUE; 
  170.         } 
  171.         $this->log_write("Error: Cannot connect to any mx hosts (".implode(", "$MXHOSTS).")\n"); 
  172.         return FALSE; 
  173.     } 
  174.  
  175.     function smtp_message($header$body
  176.     { 
  177.         fputs($this->sock, $header."\r\n".$body); 
  178.         $this->smtp_debug("> ".str_replace("\r\n""\n"."> "$header."\n> ".$body."\n> ")); 
  179.          
  180.         return TRUE; 
  181.     } 
  182.  
  183.     function smtp_eom() 
  184.     { 
  185.         fputs($this->sock, "\r\n.\r\n"); 
  186.         $this->smtp_debug(". [EOM]\n"); 
  187.          
  188.         return $this->smtp_ok(); 
  189.     } 
  190.  
  191.     function smtp_ok() 
  192.     { 
  193.         $response = str_replace("\r\n"""fgets($this->sock, 512)); 
  194.         $this->smtp_debug($response."\n"); 
  195.          
  196.         if (!preg_match("/^[23]/"$response)) { 
  197.             fputs($this->sock, "QUIT\r\n"); 
  198.             fgets($this->sock, 512); 
  199.             $this->log_write("Error: Remote host returned \"".$response."\"\n"); 
  200.             return FALSE; 
  201.         } 
  202.         return TRUE; 
  203.     } 
  204.  
  205.     function smtp_putcmd($cmd$arg = ""
  206.     { 
  207.         if ($arg != "") { 
  208.             if($cmd==""$cmd = $arg
  209.             else $cmd = $cmd." ".$arg
  210.         } 
  211.  
  212.         fputs($this->sock, $cmd."\r\n"); 
  213.         $this->smtp_debug("> ".$cmd."\n"); 
  214.  
  215.         return $this->smtp_ok(); 
  216.     } 
  217.  
  218.     function smtp_error($string
  219.     { 
  220.         $this->log_write("Error: Error occurred while ".$string.".\n"); 
  221.         return FALSE; 
  222.     } 
  223.  
  224.     function log_write($message
  225.     { 
  226.         $this->smtp_debug($message); 
  227.          
  228.         if ($this->log_file == "") { 
  229.             return TRUE; 
  230.         } 
  231.  
  232.         $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message
  233.         if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) { 
  234.             $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n"); 
  235.             return FALSE; 
  236.         } 
  237.         flock($fp, LOCK_EX); 
  238.         fputs($fp$message); 
  239.         fclose($fp); 
  240.          
  241.         return TRUE; 
  242.     } 
  243.  
  244.     function strip_comment($address
  245.     { 
  246.         $comment = "/\\([^()]*\\)/"
  247.         while (preg_match($comment$address)) { 
  248.             $address = preg_replace($comment""$address); 
  249.         } 
  250.          
  251.         return $address
  252.     } 
  253.  
  254.     function get_address($address
  255.     { 
  256.         $address = preg_replace("/([ \t\r\n])+/"""$address); 
  257.         $address = preg_replace("/^.*<(.+)>.*$/""\\1"$address); 
  258.          
  259.         return $address
  260.     } 
  261.  
  262.     function smtp_debug($message
  263.     { 
  264.         if ($this->debug) { 
  265.             echo $message."<br>"
  266.         } 
  267.     } 
  268.  
  269. function get_attach_type($image_tag) { // 
  270.  
  271.     $filedata = array(); 
  272.      
  273.     $img_file_con=fopen($image_tag,"r"); 
  274.     unset($image_data); 
  275.     while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag)))) 
  276.     $image_data.=$tem_buffer
  277.     fclose($img_file_con); 
  278.  
  279.     $filedata['context'] = $image_data
  280.     $filedata['filename']= basename($image_tag); 
  281.     $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,".")); 
  282.     switch($extension){ 
  283.         case ".gif"
  284.             $filedata['type'] = "image/gif"
  285.             break
  286.         case ".gz"
  287.             $filedata['type'] = "application/x-gzip"
  288.             break
  289.         case ".htm"
  290.             $filedata['type'] = "text/html"
  291.             break
  292.         case ".html"
  293.             $filedata['type'] = "text/html"
  294.             break
  295.         case ".jpg"
  296.             $filedata['type'] = "image/jpeg"
  297.             break
  298.         case ".tar"
  299.             $filedata['type'] = "application/x-tar"
  300.             break
  301.         case ".txt"
  302.             $filedata['type'] = "text/plain"
  303.             break
  304.         case ".zip"
  305.             $filedata['type'] = "application/zip"
  306.             break
  307.         default
  308.             $filedata['type'] = "application/octet-stream"
  309.             break
  310.     } 
  311.     return $filedata
  312.     } 
  313.  
  314. // end class 
  315. ?> 
模板无忧原创,转载请保留出处.

分享:揭秘优化php代码42种方法
1.如果一个方法可静态化,就对它做静态声明。速率可提升至4倍。 2.echo比print快。 3.使用echo的多重参数(译注:指用逗号而不是句点)代替字符串连接。 4.在执行for循环之前确定最大循环数,不要每循环一次都计算最大值。 5.注销那些不用的变量尤其是大数组

来源:模板无忧//所属分类:PHP教程/更新时间:2012-07-05
相关PHP教程