邮件乱码问题彻底解决办法

来源:互联网 发布:facebook好友推荐算法 编辑:程序博客网 时间:2024/05/16 00:39

最近由于业务需要,一直在搞系统邮件的问题,一些开源的代码虽然靠谱,但是代码冗余太多了。。。

其实自己写的邮件发送主要问题就在于邮件乱码,只要解决了这点,其实用啥都一样

邮件内容乱码

主要原因是发送header里没有设置正确的编码格式如(utf-8等)

解决:

在header里面加入Content-Type:text/html;Charset=’utf-8‘\r\n即可

注意格式正确就行

邮件主题乱码

纠结了比较久,没想到邮件subject都是加密形式发送的。不加密的话可能存在某些邮箱正常(如:gmail),某些就一堆乱码(如:网易)

解决:

$subject = "=?UTF-8?B?".base64_encode($subject)."?=";

在邮件header里面则是:Subject: ".$subject."\r\n;

这样几乎所有邮箱的乱码问题就ok了。


欢迎访问:http://www.shendoow.com(帮朋友推荐下^_^)

附上发送邮件类代码(PHP版):

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

用法:

[php] view plaincopyprint?
  1. $smtp = new MailClass($SMTP_SERV,$smtpserverport,true,$SMTP_USER,$SMTP_PASS);//这里面的一个true是表示使用身份验证,否则不使用身份验证.  
  2.   
  3. $smtp->sendmail($email$SMTP_USER$mailsubject$mailbody$mailtype);  
  4.   
  5. //具体变量根据名字和类的注释来看吧  

0 0
原创粉丝点击