用php接pop3的电子邮件,  附件rar数据总是破损的, txt则乱码,怎么解决。给位大哥给点意见,或 完整的例子, 或新的类, 或给个解决链接也可以。谢谢了

解决方案 »

  1.   

    是解码问题.我去年做过一个php 收发邮件的.需要的话 email: qimingxin0101#163.com 把#改成@索取
      

  2.   

    解码类:
    <?php
    class mime_decode {
        var $content= array();
    function mime_encode_headers($string) {
            if($string == "") return;
            if(!eregi("^([[:print:]]*)$",$string))
                $string = "=?ISO-8859-1?Q?".str_replace("+","_",str_replace("%","=",urlencode($string)))."?=";
            return $string;
        }    function decode_mime_string($string) {
            if(($pos = strpos($string,"=?")) === false) return $string;
            $string = preg_replace('|\?=\s=\?|', '?==?', $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;
            }        
            $charset = substr($string, $pos + 2, $d1);
    $this->content["charset"]=($this->content["charset"])?$this->content["charset"]:$charset;
            while(!($pos === false)) {
                $newresult .= substr($string,0,$pos);
                $string = substr($string,$pos+2,strlen($string));
                $intpos = strpos($string,"?");
                $charset = substr($string,0,$intpos);
                $enctype = strtolower(substr($string,$intpos+1,1));
                $string = substr($string,$intpos+3,strlen($string));
                $endpos = strpos($string,"?=");
                $mystring = substr($string,0,$endpos);
                $string = substr($string,$endpos+2,strlen($string));
                if($enctype == "q") {
                    $mystring = str_replace("_"," ",$mystring);
                    $mystring = $this->decode_qp($mystring);
                } else if ($enctype == "b")
                    $mystring = base64_decode($mystring);
                $newresult .= $mystring;
                $pos = strpos($string,"=?");
            }
            return $newresult.$string;
        }    function decode_header($header) {
            $headers = explode("\r\n",$header);
    //print_r($headers);
            $decodedheaders = array();
            for($i=0;$i<count($headers);$i++) {
                $thisheader = $headers[$i];
                if(strpos($thisheader,":") === false) {
                    $decodedheaders[$lasthead] .= " $thisheader";
                } else {
                    $dbpoint = strpos($thisheader,":");
                    $headname = strtolower(substr($thisheader,0,$dbpoint));
                    $headvalue = trim(substr($thisheader,$dbpoint+1));
                    if($decodedheaders[$headname] != "") $decodedheaders[$headname] .= "; $headvalue";
                    else $decodedheaders[$headname] = $headvalue;
                    $lasthead = $headname;
                }
            }
            return $decodedheaders;
        }    function fetch_structure($email) { /*
    $ARemail = array();
    $separador = "\r\n\r\n";
    $header = trim(substr($email,0,strpos($email,$separador)));
    $bodypos = strlen($header)+strlen($separador);
    $body = substr($email,$bodypos,strlen($email)-$bodypos);
    $ARemail["header"] = $header;
    $ARemail["body"] = $body;
    return $ARemail;
    */
    $ARemail = array();
    $separador = "\r\n\r\n";
            $pos = strpos($email, $separador);
            if ($pos === false) {
                echo 'Could not split header and body';
                return $ARemail;
            }
            $header = substr($email, 0, $pos);
            $body   = substr($email, $pos+strlen($separador));
    $ARemail["header"] = $header;
    $ARemail["body"] = $body;
    return $ARemail;    }    function get_names($strmail) {
            $ARfrom = array();
            $strmail = stripslashes(ereg_replace("\t","",ereg_replace("\n","",ereg_replace("\r","",$strmail))));
            if(trim($strmail) == "") return $ARfrom;        $armail = array();
            $counter = 0;  $inthechar = 0;
            $chartosplit = ",;"; $protectchar = "\""; $temp = "";
            $lt = "<"; $gt = ">";
            $closed = 1;        for($i=0;$i<strlen($strmail);$i++) {
                $thischar = $strmail[$i];
                if($thischar == $lt && $closed) $closed = 0;
                if($thischar == $gt && !$closed) $closed = 1;
                if($thischar == $protectchar) $inthechar = ($inthechar)?0:1;
                if(!(strpos($chartosplit,$thischar) === false) && !$inthechar && $closed) {
                    $armail[] = $temp; $temp = "";
                } else
                    $temp .= $thischar;
            }        if(trim($temp) != "")
                $armail[] = trim($temp);        for($i=0;$i<count($armail);$i++) {
                $thisPart = trim(eregi_replace("^\"(.*)\"$", "\\1", trim($armail[$i])));
                if($thisPart != "") {
                    if (eregi("(.*)<(.*)>", $thisPart, $regs)) {
                        $email = trim($regs[2]);
                        $name = trim($regs[1]);
                    } else {
                        if (eregi("([-a-z0-9_$+.]+@[-a-z0-9_.]+[-a-z0-9_]+)((.*))", $thisPart, $regs)) {
                            $email = $regs[1];
                            $name = $regs[2];
                        } else
                            $email = $thisPart;
                    }
                    $email = eregi_replace("^\<(.*)\>$", "\\1", $email);
                    $name = eregi_replace("^\"(.*)\"$", "\\1", trim($name));
                    $name = eregi_replace("^\((.*)\)$", "\\1", $name);
                    if ($name == "") $name = $email;
                    if ($email == "") $email = $name;
                    $ARfrom[$i]["name"] = $this->decode_mime_string($name);
                    $ARfrom[$i]["mail"] = $email;
                    unset($name);unset($email);
                }
            }
            return $ARfrom;
        }
      

  3.   

        function build_alternative_body($ctype,$body) {
            global $mime_show_html;
            $boundary = $this->get_boundary($ctype);
            $part = $this->split_parts($boundary,$body);
            $thispart = ($mime_show_html)?$part[1]:$part[0];
            $email = $this->fetch_structure($thispart);
            $header = $email["header"];
            $body = $email["body"];
            $headers = $this->decode_header($header);
            $body = $this->compile_body($body,$headers["content-transfer-encoding"]);
            return $body;
        }    function build_related_body($ctype,$body) {
            global $mime_show_html,$sid,$lid,$ix,$folder;
            $Rtype = trim(substr($ctype,strpos($ctype,"type=")+5,strlen($ctype)));        if(strpos($Rtype,";") != 0)
                $Rtype = substr($Rtype,0,strpos($Rtype,";"));
            if(substr($Rtype,0,1) == "\"" && substr($Rtype,-1) == "\"")
                $Rtype = substr($Rtype,1,strlen($Rtype)-2);        $boundary = $this->get_boundary($ctype);
            $part = $this->split_parts($boundary,$body);        for($i=0;$i<count($part);$i++) {
                $email = $this->fetch_structure($part[$i]);
                $header = $email["header"];
                $body = $email["body"];
                $headers = $this->decode_header($header);
                $ctype = $headers["content-type"];
                $cid = $headers["content-id"];
                $Actype = split(";",$headers["content-type"]);
                $types = split("/",$Actype[0]); $rctype = strtolower($Actype[0]);
                if($rctype == "multipart/alternative")
                    $msgbody = $this->build_alternative_body($ctype,$body);
                elseif($rctype == "text/plain" && strpos($headers["content-disposition"],"name") === false) {
                    $body = $this->compile_body($body,$headers["content-transfer-encoding"]);
                    $msgbody = $this->build_text_body($body);
                } elseif($rctype == "text/html" && strpos($headers["content-disposition"],"name") === false) {
                    $body = $this->compile_body($body,$headers["content-transfer-encoding"]);
                    if(!$mime_show_html) $body = $this->build_text_body(strip_tags($body));
                    $msgbody = $body;
                } else {
                    $thisattach = $this->build_attach($header,$body,$boundary,$i);
                    if($cid != "") {
                        if(substr($cid,0,1) == "<" && substr($cid,-1) == ">")
                            $cid = substr($cid,1,strlen($cid)-2);
                        $cid = "cid:$cid";
                        $thisfile = "download.php?sid=$sid&lid=$lid&folder=".urlencode($folder)."&ix=".$ix."&bound=".base64_encode($thisattach["boundary"])."&part=".$thisattach["part"]."&filename=".urlencode($thisattach["name"]);
                        $msgbody = str_replace($cid,$thisfile,$msgbody);
                    }
                }
            }
            return $msgbody;
        }    function linesize($message="", $length=70) {
            $line = explode("\r\n",$message);
            unset($message);
            for ($i=0 ;$i < count($line); $i++) {
                $line_part = explode(" ",trim($line[$i]));
                unset($buf);
                for ($e = 0; $e<count($line_part); $e++) {
                    $buf_o = $buf;
                    if ($e == 0) $buf .= $line_part[$e];
                    else $buf .= " ".$line_part[$e];
                    if (strlen($buf) > $length and $buf_o != "") {
                        $message .= "$buf_o\r\n";
                        $buf = $line_part[$e];
                    }
                }
                $message .= "$buf\r\n";
            }
            return($message);
        }
     function build_text_body($body) {
            return "\n<pre>".$this->make_link_clickable($this->linesize(htmlspecialchars($body),85))."</pre>\n";
        }    function decode_qp($text) {
            $text = quoted_printable_decode($text);
            /*
            $text = str_replace("\r","",$text);
            $text = ereg_replace("=\n", "", $text);
            $text = str_replace("\n","\r\n",$text);
            */
            $text = ereg_replace("=\r", "\r", $text);
            return $text;
        }    function make_link_clickable($text){
            $text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
                "<a class=\"autolink\" href=\"\\1://\\2\\3\" target=\"_new\">\\1://\\2\\3</a>", $text);
            $text = eregi_replace("([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,3})","<a class=\"autolink\"  href=\"newmsg.php?mailto=\\1&nameto=\\1\">\\1</a>", $text);
            return $text;
        }
      

  4.   

        function process_message($header,$body) {
            global $mime_show_html;
            $mail_info = $this->get_mail_info($header);        $ctype = $mail_info["content-type"];
            $ctenc = $mail_info["content-transfer-encoding"];        if($ctype == "") $ctype = "text/plain";        $type = $ctype;        $ctype = split(";",$ctype);
            $types = split("/",$ctype[0]);
    /*
            preg_match("/^charset=[\"|\'](.*)[\"|\']$/",trim(strtolower($ctype[1])),$bcharset);
    $maincharset = $bcharset[1];
    */
            $maintype = strtolower($types[0]);
            $subtype = strtolower($types[1]);
    /*
    echo "<pre>";
    echo $maintype."--".$subtype;
    echo "</pre>";
    */
            switch($maintype) {
    case "text":
    $body = $this->compile_body($body,$ctenc);
    switch($subtype) {
    case "html":
    $mime_show_html=1;
    //  preg_match("/^content=\"text\/html;[\s]+charset=(\s+)\"$/",strtolower($body),$bcharset);
    //  $maincharset = $bcharset[1];
    if(!$mime_show_html)
    $body = $this->build_text_body(strip_tags($body));
    $msgbody = $body;
    break;
    default:
    $mime_show_html=0;
    $msgbody = $this->build_text_body($body);
    break;
    }
    break;
    case "multipart":
    switch($subtype) {
    case "mixed":
    $boundary = $this->get_boundary($type);
    $part = $this->split_parts($boundary,$body);
    for($i=0;$i<count($part);$i++) {
    $thispart = trim($part[$i]);
    if($thispart != "") {
    $email = $this->fetch_structure($thispart);
    $header = $email["header"];
    $body = $email["body"];
    $headers = $this->decode_header($header);
    $ctype = $headers["content-type"];

    $Actype = split(";",$headers["content-type"]);
    $types = split("/",$Actype[0]); $rctype = strtolower($Actype[0]);

    if($rctype == "multipart/alternative")
    $msgbody = $this->build_alternative_body($ctype,$body);
    elseif($rctype == "text/plain" && strpos($headers["content-disposition"],"name") === false) {
    $msgbody = $this->build_text_body($this->compile_body($body,$headers["content-transfer-encoding"]));
    } elseif($rctype == "text/html" && strpos($headers["content-disposition"],"name") === false) {
    $body = $this->compile_body($body,$headers["content-transfer-encoding"]);
    if(!$mime_show_html)
    $body = $this->build_text_body(strip_tags($body));
    $msgbody = $body;
    } elseif($rctype == "multipart/related" && strpos($headers["content-disposition"],"name") === false) {
    $msgbody = $this->build_related_body($ctype,$body);
    } else {
    $thisattach = $this->build_attach($header,$body,$boundary,$i);
    }
    }
    }
    break;
    case "alternative":
    /*
    foreach($ctype as $bound){
    if(preg_match("/^boundary/",strtolower($bound))){
    break;
    }
    }
    $msgbody = $this->build_alternative_body($bound,$body);
    */
    $msgbody = $this->build_alternative_body($ctype[1],$body);
    break;
    case "related":
    $msgbody = $this->build_related_body($type,$body);
    break;
    default:
    $thisattach = $this->build_attach($header,$body,"",0);
    }
    break;
    default:
    $thisattach = $this->build_attach($header,$body,"",0);
            }
    $msgbody=($maincharset!="utf-8"&&$maincharset)?iconv($maincharset,"utf-8//IGNORE",$msgbody):$msgbody;
            return $msgbody;
        }    function build_attach($header,$body,$boundary,$part) {
            global $mail,$temporary_directory,$userfolder;        $headers = $this->decode_header($header);
            $cdisp = $headers["content-disposition"];
            $ctype = $headers["content-type"]; $ctype2 = explode(";",$ctype); $ctype2 = $ctype2[0];
            
            $Atype = split("/",$ctype);
            $Acdisp = split(";",$cdisp);        $tenc = $headers["content-transfer-encoding"];        if($temp) $dir_to_save = $userfolder; //"temporary_files/";        if($Atype[0] == "message") {
                $divpos = strpos($body,"\n\r");
                $attachheader = substr($body,0,$divpos);
                $attachheaders = $this->decode_header($attachheader);
                $filename = $this->decode_mime_string($attachheaders["subject"]);
                if($filename == "")
                    $filename = uniqid("");
                $filename = substr(ereg_replace("[^A-Za-z0-9]","_",$filename),0,20).".eml";
            } else {
                $fname = $Acdisp[1];
                $filename = substr($fname,strpos($fname,"filename=")+9,strlen($fname));
                if($filename == "")
                    $filename = substr($ctype,strpos($ctype,"name=")+5,strlen($ctype));
                if(substr($filename,0,1) == "\"" && substr($filename,-1) == "\"")
                    $filename = substr($filename,1,strlen($filename)-2);
                $filename = $this->decode_mime_string($filename);
            }
            if($Atype[0] != "message")
                $body = $this->compile_body($body,$tenc);
            $indice = count($this->content["attachments"]);
            $this->content["attachments"][$indice]["name"] = $filename;
            $this->content["attachments"][$indice]["size"] = strlen($body);
            $this->content["attachments"][$indice]["temp"] = $temp;
            $this->content["attachments"][$indice]["content-type"] = $ctype2; //$Atype[0];
            $this->content["attachments"][$indice]["content-disposition"] = $Acdisp[0];
            $this->content["attachments"][$indice]["boundary"] = $boundary;
            $this->content["attachments"][$indice]["part"] = $part;
          //  $this->content["attachments"][$indice]["body"] = $body; //add 2007-11-09
            return $this->content["attachments"][$indice];
        }    function compile_body($body,$enctype) {
            $enctype = explode(" ",$enctype); $enctype = $enctype[0];
            if(strtolower($enctype) == "base64")
                $body = base64_decode($body);
            elseif(strtolower($enctype) == "quoted-printable")
                $body = $this->decode_qp($body);
            return $body;    }

    function down_attach($boundary,$body,$part){
    $getpart =$this->split_parts($boundary,$body);
    $thispart = trim($getpart[$part]);
    if($thispart != "") {
    $email = $this->fetch_structure($thispart);
    $header = $email["header"];
    $body = $email["body"];
    $this->download_attach($header,$body);
    }
    }
      

  5.   

    非常谢谢kemy88,很想给分,只是自己都是穷光蛋,真的很不好意思