【轉自loveifa】PHP 獲取郵箱未讀郵件的方法 根據receivemail改編而來,已解決亂碼,編碼問題

  1. receivemail是老外寫的,難免不支持中文,研究了一個下午終於成功解決了這個問題,寫出來給大家分享下  
  1.   
  1. <?  
  2. /* 
  3.  * File: example.php 
  4.  * Description: Received Mail Example 
  5.  * Created: 01-03-2006 
  6.  * Author: Mitul Koradia 
  7.  * Email: [email protected] 
  8.  * Cell : +91 9825273322 
  9.  */  
  10.  function test($strHead){   
  11. if(ereg("=\?.{0,}\?[Bb]\?",$strHead)){   
  12.   $arrHead=split("=\?.{0,}\?[Bb]\?",$strHead);   
  13.   while(list($key,$value)=each($arrHead)){   
  14.     if(ereg("\?=",$value)){   
  15.       $arrTemp=split("\?=",$value);   
  16.       $arrTemp[0]=base64_decode($arrTemp[0]);   
  17.       $arrHead[$key]=join("",$arrTemp);   
  18.     }   
  19.   }   
  20.   $strHead=join("",$arrHead);   
  21. }   
  22.   return $strHead;   
  23. }   
  24.   
  25. function is_utf8($string) {  
  26.       
  27.     // From http://w3.org/International/questions/qa-forms-utf-8.html  
  28.     return preg_match('%^(?:  
  29.           [\x09\x0A\x0D\x20-\x7E]            # ASCII  
  30.         | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte  
  31.         |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs  
  32.         | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte  
  33.         |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates  
  34.         |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3  
  35.         | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15  
  36.         |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16  
  37.     )*$%xs', $string);  
  38.       
  39. }  
  40.   
  41.   
  42.    
  43.    
  44. include("receivemail.class.php");  
  45. // Creating a object of reciveMail Class  
  46. $objnew receiveMail('[email protected]','xxx','[email protected]','pop.qq.com','pop3','110',false);  
  47.   
  48. //Connect to the Mail Box  
  49. $obj->connect();         //If connection fails give error message and exit  
  50.   
  51. // Get Total Number of Unread Email in mail box  
  52. $tot=$obj->getTotalMails(); //Total Mails in Inbox Return integer value  
  53.   
  54. echo "Total Mails:: $tot<br>";  
  55.   
  56. for($i=$tot;$i>0;$i--)  
  57. {  
  58.     $head=$obj->getHeaders($i);  // Get Header Info Return Array Of Headers **Array Keys are (subject,to,toOth,toNameOth,from,fromName)  
  59.     $s=test($head['subject']);  
  60.     if(is_utf8($s)=='1'){  
  61.     echo iconv('UTF-8','gb2312',$s) ;   
  62.     }else{  
  63.         echo $s;  
  64.     }  
  65.     echo "<br>";  
  66.     //echo "Subjects :: ".test($head['subject'])."<br>";  
  67.        
  68.     echo "對方郵箱".$head['from']."<br>";  
  69.       
  70.     $ss=test($head['fromName']);  
  71.     if(is_utf8($ss)=='1'){  
  72.     echo iconv('UTF-8','gb2312',$ss) ;   
  73.     }else{  
  74.         echo $ss;  
  75.     }  
  76.     //echo "FromName :: ".$head['fromName']."<br>";  
  77.    
  78.     echo "<br>*******************************************************************************************<BR>";  
  79.     //echo $obj->getBody($i);  // Get Body Of Mail number Return String Get Mail id in interger  
  80.       
  81.     //$str=$obj->GetAttach($i,"./"); // Get attached File from Mail Return name of file in comma separated string  args. (mailid, Path to store file)  
  82.     //$ar=explode(",",$str);  
  83. //  foreach($ar as $key=>$value)  
  84. //      echo ($value=="")?"":"Atteched File :: ".$value."<br>";  
  85. //  echo "<br>------------------------------------------------------------------------------------------<BR>";  
  86.       
  87.     //$obj->deleteMails($i); // Delete Mail from Mail box  
  88. }  
  89. $obj->close_mailbox();   //Close Mail Box  
  90.   
  91. ?>  



[html] view plaincopy
  1. <?php  
  2. // Main ReciveMail Class File - Version 1.1 (02-06-2009)  
  3. /*  
  4.  * File: recivemail.class.php  
  5.  * Description: Reciving mail With Attechment  
  6.  * Version: 1.1  
  7.  * Created: 01-03-2006  
  8.  * Modified: 02-06-2009  
  9.  * Author: Mitul Koradia  
  10.  * Email: [email protected]  
  11.  * Cell : +91 9825273322  
  12.  */  
  13.    
  14. /***************** Changes *********************  
  15. *  
  16. * 1) Added feature to retrive embedded attachment - Changes provided by. Antti <anttiantti83@gmail.com>  
  17. * 2) Added SSL Supported mailbox.  
  18. *  
  19. **************************************************/  
  20.   
  21. class receiveMail  
  22. {  
  23.     var $server='';  
  24.     var $username='';  
  25.     var $password='';  
  26.       
  27.     var $marubox='';                      
  28.       
  29.     var $email='';            
  30.       
  31.     function receiveMail($username,$password,$EmailAddress,$mailserver='localhost',$servertype='pop',$port='110',$ssl = false) //Constructure  
  32.     {  
  33.         if($servertype=='imap')  
  34.         {  
  35.             if($port=='') $port='143';   
  36.             $strConnect='{'.$mailserver.':'.$port. '}INBOX';   
  37.         }  
  38.         else  
  39.         {  
  40.             $strConnect='{'.$mailserver.':'.$port. '/pop3'.($ssl ? "/ssl" : "").'}INBOX';   
  41.         }  
  42.         $this->server            =   $strConnect;  
  43.         $this->username          =   $username;  
  44.         $this->password          =   $password;  
  45.         $this->email         =   $EmailAddress;  
  46.     }  
  47.     function connect() //Connect To the Mail Box  
  48.     {  
  49.         $this->marubox=@imap_open($this->server,$this->username,$this->password);  
  50.           
  51.         if(!$this->marubox)  
  52.         {  
  53.             echo "Error: Connecting to mail server";  
  54.             exit;  
  55.         }  
  56.     }  
  57.       
  58.     function getHeaders($mid) // Get Header info  
  59.     {  
  60.         if(!$this->marubox)  
  61.             return false;  
  62.   
  63.         $mail_header=imap_header($this->marubox,$mid);  
  64.         $sender=$mail_header->from[0];  
  65.         $sender_replyto=$mail_header->reply_to[0];  
  66.         if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster')  
  67.         {  
  68.             $mail_details=array(  
  69.                     'from'=>strtolower($sender->mailbox).'@'.$sender->host,  
  70.                     'fromName'=>$sender->personal,  
  71.                     'toOth'=>strtolower($sender_replyto->mailbox).'@'.$sender_replyto->host,  
  72.                     'toNameOth'=>$sender_replyto->personal,  
  73.                     'subject'=>$mail_header->subject,  
  74.                     'to'=>strtolower($mail_header->toaddress)  
  75.                 );  
  76.         }  
  77.         return $mail_details;  
  78.     }  
  79.     function get_mime_type(&$structure) //Get Mime type Internal Private Use  
  80.     {   
  81.         $primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");   
  82.           
  83.         if($structure->subtype) {   
  84.             return $primary_mime_type[(int) $structure->type] . '/' . $structure->subtype;   
  85.         }   
  86.         return "TEXT/PLAIN";   
  87.     }   
  88.     function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) //Get Part Of Message Internal Private Use  
  89.     {   
  90.         if(!$structure) {   
  91.             $structure = imap_fetchstructure($stream, $msg_number);   
  92.         }   
  93.         if($structure) {   
  94.             if($mime_type == $this->get_mime_type($structure))  
  95.             {   
  96.                 if(!$part_number)   
  97.                 {   
  98.                     $part_number = "1";   
  99.                 }   
  100.                 $text = imap_fetchbody($stream, $msg_number, $part_number);   
  101.                 if($structure->encoding == 3)   
  102.                 {   
  103.                     return imap_base64($text);   
  104.                 }   
  105.                 else if($structure->encoding == 4)   
  106.                 {   
  107.                     return imap_qprint($text);   
  108.                 }   
  109.                 else  
  110.                 {   
  111.                     return $text;   
  112.                 }   
  113.             }   
  114.             if($structure->type == 1) /* multipart */   
  115.             {   
  116.                 while(list($index, $sub_structure) = each($structure->parts))  
  117.                 {   
  118.                     if($part_number)  
  119.                     {   
  120.                         $prefix = $part_number . '.';   
  121.                     }   
  122.                     $data = $this->get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1));   
  123.                     if($data)  
  124.                     {   
  125.                         return $data;   
  126.                     }   
  127.                 }   
  128.             }   
  129.         }   
  130.         return false;   
  131.     }   
  132.     function getTotalMails() //Get Total Number off Unread Email In Mailbox  
  133.     {  
  134.         if(!$this->marubox)  
  135.             return false;  
  136.   
  137.         $headers=imap_headers($this->marubox);  
  138.         return count($headers);  
  139.     }  
  140.     function GetAttach($mid,$path) // Get Atteced File from Mail  
  141.     {  
  142.         if(!$this->marubox)  
  143.             return false;  
  144.   
  145.         $struckture = imap_fetchstructure($this->marubox,$mid);  
  146.         $ar="";  
  147.         if($struckture->parts)  
  148.         {  
  149.             foreach($struckture->parts as $key => $value)  
  150.             {  
  151.                 $enc=$struckture->parts[$key]->encoding;  
  152.                 if($struckture->parts[$key]->ifdparameters)  
  153.                 {  
  154.                     $name=$struckture->parts[$key]->dparameters[0]->value;  
  155.                     $message = imap_fetchbody($this->marubox,$mid,$key+1);  
  156.                     if ($enc == 0)  
  157.                         $message = imap_8bit($message);  
  158.                     if ($enc == 1)  
  159.                         $message = imap_8bit ($message);  
  160.                     if ($enc == 2)  
  161.                         $message = imap_binary ($message);  
  162.                     if ($enc == 3)  
  163.                         $message = imap_base64 ($message);   
  164.                     if ($enc == 4)  
  165.                         $message = quoted_printable_decode($message);  
  166.                     if ($enc == 5)  
  167.                         $message = $message;  
  168.                     $fp=fopen($path.$name,"w");  
  169.                     fwrite($fp,$message);  
  170.                     fclose($fp);  
  171.                     $ar=$ar.$name.",";  
  172.                 }  
  173.                 // Support for embedded attachments starts here  
  174.                 if($struckture->parts[$key]->parts)  
  175.                 {  
  176.                     foreach($struckture->parts[$key]->parts as $keyb => $valueb)  
  177.                     {  
  178.                         $enc=$struckture->parts[$key]->parts[$keyb]->encoding;  
  179.                         if($struckture->parts[$key]->parts[$keyb]->ifdparameters)  
  180.                         {  
  181.                             $name=$struckture->parts[$key]->parts[$keyb]->dparameters[0]->value;  
  182.                             $partnro = ($key+1).".".($keyb+1);  
  183.                             $message = imap_fetchbody($this->marubox,$mid,$partnro);  
  184.                             if ($enc == 0)  
  185.                                    $message = imap_8bit($message);  
  186.                             if ($enc == 1)  
  187.                                    $message = imap_8bit ($message);  
  188.                             if ($enc == 2)  
  189.                                    $message = imap_binary ($message);  
  190.                             if ($enc == 3)  
  191.                                    $message = imap_base64 ($message);  
  192.                             if ($enc == 4)  
  193.                                    $message = quoted_printable_decode($message);  
  194.                             if ($enc == 5)  
  195.                                    $message = $message;  
  196.                             $fp=fopen($path.$name,"w");  
  197.                             fwrite($fp,$message);  
  198.                             fclose($fp);  
  199.                             $ar=$ar.$name.",";  
  200.                         }  
  201.                     }  
  202.                 }                 
  203.             }  
  204.         }  
  205.         $ar=substr($ar,0,(strlen($ar)-1));  
  206.         return $ar;  
  207.     }  
  208.     function getBody($mid) // Get Message Body  
  209.     {  
  210.         if(!$this->marubox)  
  211.             return false;  
  212.   
  213.         $body = $this->get_part($this->marubox, $mid, "TEXT/HTML");  
  214.         if ($body == "")  
  215.             $body = $this->get_part($this->marubox, $mid, "TEXT/PLAIN");  
  216.         if ($body == "") {   
  217.             return "";  
  218.         }  
  219.         return $body;  
  220.     }  
  221.     function deleteMails($mid) // Delete That Mail  
  222.     {  
  223.         if(!$this->marubox)  
  224.             return false;  
  225.       
  226.         imap_delete($this->marubox,$mid);  
  227.     }  
  228.     function close_mailbox() //Close Mail Box  
  229.     {  
  230.         if(!$this->marubox)  
  231.             return false;  
  232.   
  233.         imap_close($this->marubox,CL_EXPUNGE);  
  234.     }  
  235. }  
  236. ?>  

目前僅僅支持ansi
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章