有时得到附件。有时不!使用imap

时间:2015-08-07 16:32:11

标签: php imap gmail-imap

我有一个脚本可以下载所有pdf文件并将其保存在库中... 即时通讯使用Gmail帐户..启用Imap; 使用centOS 5.11,openssl版本:1.0.1g:

为什么有时它会起作用..为什么有时候不会出错?

错误说

PHP Warning:  imap_open(): Couldn't open stream    {imap.gmail.com:993/imap/ssl}INBOX
No such host as imap.gmail.comPHP Notice:  Unknown: No such host as imap.gmail.com (errflg=2)

这是代码:

<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'xxxxxx@gmail.com';
$password = 'password';
$savedirpath = "/myLibrary";
$type = 'ReadAttachment';
$obj = new $type;
$obj->getdata($hostname ,$username ,$password,$savedirpath);
class ReadAttachment
{
function getdecodevalue($message,$coding) {
    switch($coding) {
        case 0:
        case 1:
            $message = imap_8bit($message);
            break;
        case 2:
            $message = imap_binary($message);
            break;
        case 3:
        case 5:
            $message=imap_base64($message);
            break;
        case 4:
            $message = imap_qprint($message);
            break;
    }
    return $message;
}


function getdata($hostname ,$username ,$password,$savedirpath) {

    $savedirpath = str_replace('\\', '/', $savedirpath);
    if (substr($savedirpath, strlen($savedirpath) - 1) != '/') {
        $savedirpath .= '/';
    }
    $mbox = imap_open ($hostname , $username , $password) or die("can't connect: " . imap_last_error());
    $message = array();
    $message["attachment"]["type"][0] = "text";
    $message["attachment"]["type"][1] = "multipart";
    $message["attachment"]["type"][2] = "message";
    $message["attachment"]["type"][3] = "application";
    $message["attachment"]["type"][4] = "audio";
    $message["attachment"]["type"][5] = "image";
    $message["attachment"]["type"][6] = "video";
    $message["attachment"]["type"][7] = "other";

            $MC = imap_check($mbox);
            $result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
            $jk = 1;
            foreach ($result as $overview) {


                $date= strtotime($overview->date);
                $unixDate = date("YmdHis", $date);
                $from= $overview->from;




                    $structure = imap_fetchstructure($mbox, $jk);                                
                    $parts = (isset($structure->parts) ? $structure->parts : false);
                    $fpos=2;

                    for($i = 1; $i < count($parts); $i++) {

                        $message["pid"][$i] = ($i);
                        $part = $parts[$i];

                        if($part->disposition == "ATTACHMENT") {    

                            $message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
                            $message["subtype"][$i] = strtolower($part->subtype);
                            $ext=$part->subtype;
                            $params = $part->dparameters;
                            $filename=$part->dparameters[0]->value;
                            $mege="";
                            $data="";
                            $mege = imap_fetchbody($mbox,$jk,$fpos);  
                            $filename="$filename";
                            $extension=pathinfo($filename);

                            $file_extension = $extension['extension'];
                            if($file_extension === "pdf")
                            {
                                $new_filename = $filename;
                                $fp=fopen($savedirpath.$new_filename,'w');
                                $data=$this->getdecodevalue($mege,$part->type); 
                                fputs($fp,$data);
                                fclose($fp);
                                $fpos+=1;                                   
                            }
                        }                   
                    } imap_delete($mbox,$jk);
                $jk++;              

            }       

        imap_expunge($mbox);
        imap_errors() ;
        imap_close($mbox);


}
}

?>

0 个答案:

没有答案