- receivemail是老外写的,难免不支持中文,研究了一个下午终于成功解决了这个问题,写出来给大家分享下
- <?
- /*
- * File: example.php
- * Description: Received Mail Example
- * Created: 01-03-2006
- * Author: Mitul Koradia
- * Email: mitulkoradia@gmail.com
- * Cell : +91 9825273322
- */
- function test($strHead){
- if(ereg("=\?.{0,}\?[Bb]\?",$strHead)){
- $arrHead=split("=\?.{0,}\?[Bb]\?",$strHead);
- while(list($key,$value)=each($arrHead)){
- if(ereg("\?=",$value)){
- $arrTemp=split("\?=",$value);
- $arrTemp[0]=base64_decode($arrTemp[0]);
- $arrHead[$key]=join("",$arrTemp);
- }
- }
- $strHead=join("",$arrHead);
- }
- return $strHead;
- }
- function is_utf8($string) {
- // From http://w3.org/International/questions/qa-forms-utf-8.html
- return preg_match('%^(?:
- [\x09\x0A\x0D\x20-\x7E] # ASCII
- | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
- | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
- | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
- | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
- | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
- | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
- | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
- )*$%xs', $string);
- }
- include("receivemail.class.php");
- // Creating a object of reciveMail Class
- $obj= new receiveMail('93131408@qq.com','xxx','93131408@qq.com','pop.qq.com','pop3','110',false);
- //Connect to the Mail Box
- $obj->connect(); //If connection fails give error message and exit
- // Get Total Number of Unread Email in mail box
- $tot=$obj->getTotalMails(); //Total Mails in Inbox Return integer value
- echo "Total Mails:: $tot<br>";
- for($i=$tot;$i>0;$i--)
- {
- $head=$obj->getHeaders($i); // Get Header Info Return Array Of Headers **Array Keys are (subject,to,toOth,toNameOth,from,fromName)
- $s=test($head['subject']);
- if(is_utf8($s)=='1'){
- echo iconv('UTF-8','gb2312',$s) ;
- }else{
- echo $s;
- }
- echo "<br>";
- //echo "Subjects :: ".test($head['subject'])."<br>";
- echo "对方邮箱".$head['from']."<br>";
- $ss=test($head['fromName']);
- if(is_utf8($ss)=='1'){
- echo iconv('UTF-8','gb2312',$ss) ;
- }else{
- echo $ss;
- }
- //echo "FromName :: ".$head['fromName']."<br>";
- echo "<br>*******************************************************************************************<BR>";
- //echo $obj->getBody($i); // Get Body Of Mail number Return String Get Mail id in interger
- //$str=$obj->GetAttach($i,"./"); // Get attached File from Mail Return name of file in comma separated string args. (mailid, Path to store file)
- //$ar=explode(",",$str);
- // foreach($ar as $key=>$value)
- // echo ($value=="")?"":"Atteched File :: ".$value."<br>";
- // echo "<br>------------------------------------------------------------------------------------------<BR>";
- //$obj->deleteMails($i); // Delete Mail from Mail box
- }
- $obj->close_mailbox(); //Close Mail Box
- ?>
- <?php
- // Main ReciveMail Class File - Version 1.1 (02-06-2009)
- /*
- * File: recivemail.class.php
- * Description: Reciving mail With Attechment
- * Version: 1.1
- * Created: 01-03-2006
- * Modified: 02-06-2009
- * Author: Mitul Koradia
- * Email: mitulkoradia@gmail.com
- * Cell : +91 9825273322
- */
- /***************** Changes *********************
- *
- * 1) Added feature to retrive embedded attachment - Changes provided by. Antti <anttiantti83@gmail.com>
- * 2) Added SSL Supported mailbox.
- *
- **************************************************/
- class receiveMail
- {
- var $server='';
- var $username='';
- var $password='';
- var $marubox='';
- var $email='';
- function receiveMail($username,$password,$EmailAddress,$mailserver='localhost',$servertype='pop',$port='110',$ssl = false) //Constructure
- {
- if($servertype=='imap')
- {
- if($port=='') $port='143';
- $strConnect='{'.$mailserver.':'.$port. '}INBOX';
- }
- else
- {
- $strConnect='{'.$mailserver.':'.$port. '/pop3'.($ssl ? "/ssl" : "").'}INBOX';
- }
- $this->server = $strConnect;
- $this->username = $username;
- $this->password = $password;
- $this->email = $EmailAddress;
- }
- function connect() //Connect To the Mail Box
- {
- $this->marubox=@imap_open($this->server,$this->username,$this->password);
- if(!$this->marubox)
- {
- echo "Error: Connecting to mail server";
- exit;
- }
- }
- function getHeaders($mid) // Get Header info
- {
- if(!$this->marubox)
- return false;
- $mail_header=imap_header($this->marubox,$mid);
- $sender=$mail_header->from[0];
- $sender_replyto=$mail_header->reply_to[0];
- if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster')
- {
- $mail_details=array(
- 'from'=>strtolower($sender->mailbox).'@'.$sender->host,
- 'fromName'=>$sender->personal,
- 'toOth'=>strtolower($sender_replyto->mailbox).'@'.$sender_replyto->host,
- 'toNameOth'=>$sender_replyto->personal,
- 'subject'=>$mail_header->subject,
- 'to'=>strtolower($mail_header->toaddress)
- );
- }
- return $mail_details;
- }
- function get_mime_type(&$structure) //Get Mime type Internal Private Use
- {
- $primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");
- if($structure->subtype) {
- return $primary_mime_type[(int) $structure->type] . '/' . $structure->subtype;
- }
- return "TEXT/PLAIN";
- }
- function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) //Get Part Of Message Internal Private Use
- {
- if(!$structure) {
- $structure = imap_fetchstructure($stream, $msg_number);
- }
- if($structure) {
- if($mime_type == $this->get_mime_type($structure))
- {
- if(!$part_number)
- {
- $part_number = "1";
- }
- $text = imap_fetchbody($stream, $msg_number, $part_number);
- if($structure->encoding == 3)
- {
- return imap_base64($text);
- }
- else if($structure->encoding == 4)
- {
- return imap_qprint($text);
- }
- else
- {
- return $text;
- }
- }
- if($structure->type == 1) /* multipart */
- {
- while(list($index, $sub_structure) = each($structure->parts))
- {
- if($part_number)
- {
- $prefix = $part_number . '.';
- }
- $data = $this->get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1));
- if($data)
- {
- return $data;
- }
- }
- }
- }
- return false;
- }
- function getTotalMails() //Get Total Number off Unread Email In Mailbox
- {
- if(!$this->marubox)
- return false;
- $headers=imap_headers($this->marubox);
- return count($headers);
- }
- function GetAttach($mid,$path) // Get Atteced File from Mail
- {
- if(!$this->marubox)
- return false;
- $struckture = imap_fetchstructure($this->marubox,$mid);
- $ar="";
- if($struckture->parts)
- {
- foreach($struckture->parts as $key => $value)
- {
- $enc=$struckture->parts[$key]->encoding;
- if($struckture->parts[$key]->ifdparameters)
- {
- $name=$struckture->parts[$key]->dparameters[0]->value;
- $message = imap_fetchbody($this->marubox,$mid,$key+1);
- if ($enc == 0)
- $message = imap_8bit($message);
- if ($enc == 1)
- $message = imap_8bit ($message);
- if ($enc == 2)
- $message = imap_binary ($message);
- if ($enc == 3)
- $message = imap_base64 ($message);
- if ($enc == 4)
- $message = quoted_printable_decode($message);
- if ($enc == 5)
- $message = $message;
- $fp=fopen($path.$name,"w");
- fwrite($fp,$message);
- fclose($fp);
- $ar=$ar.$name.",";
- }
- // Support for embedded attachments starts here
- if($struckture->parts[$key]->parts)
- {
- foreach($struckture->parts[$key]->parts as $keyb => $valueb)
- {
- $enc=$struckture->parts[$key]->parts[$keyb]->encoding;
- if($struckture->parts[$key]->parts[$keyb]->ifdparameters)
- {
- $name=$struckture->parts[$key]->parts[$keyb]->dparameters[0]->value;
- $partnro = ($key+1).".".($keyb+1);
- $message = imap_fetchbody($this->marubox,$mid,$partnro);
- if ($enc == 0)
- $message = imap_8bit($message);
- if ($enc == 1)
- $message = imap_8bit ($message);
- if ($enc == 2)
- $message = imap_binary ($message);
- if ($enc == 3)
- $message = imap_base64 ($message);
- if ($enc == 4)
- $message = quoted_printable_decode($message);
- if ($enc == 5)
- $message = $message;
- $fp=fopen($path.$name,"w");
- fwrite($fp,$message);
- fclose($fp);
- $ar=$ar.$name.",";
- }
- }
- }
- }
- }
- $ar=substr($ar,0,(strlen($ar)-1));
- return $ar;
- }
- function getBody($mid) // Get Message Body
- {
- if(!$this->marubox)
- return false;
- $body = $this->get_part($this->marubox, $mid, "TEXT/HTML");
- if ($body == "")
- $body = $this->get_part($this->marubox, $mid, "TEXT/PLAIN");
- if ($body == "") {
- return "";
- }
- return $body;
- }
- function deleteMails($mid) // Delete That Mail
- {
- if(!$this->marubox)
- return false;
- imap_delete($this->marubox,$mid);
- }
- function close_mailbox() //Close Mail Box
- {
- if(!$this->marubox)
- return false;
- imap_close($this->marubox,CL_EXPUNGE);
- }
- }
- ?>
目前仅仅支持ansi