class myimap { var $username=""; var $userpwd=""; var $hostname=""; var $port=0; var $connection=0; //是否连接 var $state="disconnected"; //连接状态 var $greeting=""; var $must_update=0; var $instream=0;
function open() { if ($this->port==110) $this->instream=imap_open("{$this->hostname/pop3:110}inbox",$this->username,$this->userpwd); else $this->instream=imap_open("{$this->hostname/imap:143}inbox",$this->username,$this->userpwd);
function decode_mime_string ($string) { $pos = strpos($string, '=?'); if (!is_int($pos)) { return $string; }
$preceding = substr($string, 0, $pos); // save any preceding text
$search = substr($string, $pos+2, 75); // the mime header spec says this is the longest a single encoded word can be $d1 = strpos($search, '?'); if (!is_int($d1)) { return $string; }
function get_barefrom($user, $server) { $barefrom = "$[email protected]$real_server";
return $barefrom; }
function get_structure($msg_num) { $structure=imap_fetchstructure($this->instream,$msg_num); //echo gettype($structure); return $structure; }
function proc_structure($msg_part, $part_no, $msg_num) { if ($msg_part->ifdisposition) { // see if it has a disposition // the only thing i know of that this // would be used for would be an attachment // lets check anyway if ($msg_part->disposition == "attachment") { // if it is an attachment, then we let people download it // first see if they sent a filename $att_name = "unknown"; for ($lcv = 0; $lcv < count($msg_part->parameters); $lcv++) { $param = $msg_part->parameters[$lcv];
// you could give a link to download the attachment here.... echo '<a href="'.$att_name.'">'.$att_name.'</a><br>'; $fp=fopen(".//$att_name","w+"); fputs($fp,imap_base64(imap_fetchbody($this->instream,$msg_num,$part_no))); fclose($fp); } else { // i guess it is used for something besides attachments???? } } else { // not an attachment, lets see what this part is... switch ($msg_part->type) { case typetext: $mime_type = "text"; break; case typemultipart: $mime_type = "multipart"; // hey, why not use this function to deal with all the parts // of this multipart part :) for ($i = 0; $i < count($msg_part->parts); $i++) { if ($part_no != "") { $part_no = $part_no."."; } for ($i = 0; $i < count($msg_part->parts); $i++) { $this->proc_structure($msg_part->parts[$i], $part_no.($i + 1), $msg_num); } } break; case typemessage: $mime_type = "message"; break; case typeapplication: $mime_type = "application"; break; case typeaudio: $mime_type = "audio"; break; case typeimage: $mime_type = "image"; break; case typevideo: $mime_type = "video"; break; case typemodel: $mime_type = "model"; break; default: $mime_type = "unknown"; // hmmm.... }
// decide what you what to do with this part // if you want to show it, figure out the encoding and echo away switch ($msg_part->encoding) { case encbase64: // use imap_base64 to decode $fp=fopen(".//$att_name","w+"); fputs($fp,imap_base64(imap_fetchbody($this->instream,$msg_num,$part_no))); fclose($fp); break; case encquotedprintable: // use imap_qprint to decode //echo ereg_replace("/n","<br>",imap_qprint(imap_fetchbody($this->instream,$msg_num,$part_no))); echo '<pre>'.imap_qprint(imap_fetchbody($this->instream,$msg_num,$part_no)).'</pre>'; break; case encother: // not sure if this needs decoding at all break; default: } } }