2012-02-18 91 views
0

我試圖使用IMAP功能分離從收件箱收到的附件並保存到指定的服務器目錄。PHP的IMAP功能分離附件並保存到目錄

我正在爲所有UNSEEN消息執行此操作,但問題在於它僅適用於一條消息。

下面是代碼(我刪除了$主機,$登錄,出於顯而易見的原因$ password變量):

$type = 'ReadAttachment'; 
    $obj = new $type; 
    $obj->getdata($host,$login,$password,$savedirpath,$delete_emails=false); 
    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($host,$login,$password,$savedirpath,$delete_emails=false, $read_type="UNSEEN") { 
      // make sure save path has trailing slash (/) 
      //print_r("test"); 
      $savedirpath = str_replace('\\', '/', $savedirpath); 
      if (substr($savedirpath, strlen($savedirpath) - 1) != '/') { 
       $savedirpath .= '/'; 
      } 

      $mbox = imap_open ($host, $login, $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"; 
      //print_r($message); 
      $emails = imap_search($mbox,$read_type) or die(print_r(imap_last_error())); 
      print_r($emails); 

      $e = imap_search($mbox,$read_type, SE_UID) or die(print_r(imap_last_error())); 
      print_r($e); 
      $i=0; 
      foreach($emails as $email_number) { 
       $structure = imap_fetchstructure($mbox, $e[$i] , FT_UID) or die(print_r(imap_last_error())); 
       $parts = $structure->parts; 
       $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,$email_number,$fpos); 
         $filename="$filename"; 
         $fp=fopen($savedirpath.$filename,"w"); 
         $data=$this->getdecodevalue($mege,$part->type); 
         //print_r($mege); 
         fputs($fp,$data); 
         fclose($fp); 
         $fpos+=1; 
        } 
       } 
       ++$i; 
      } 
      // imap_expunge deletes all tagged messages 

      imap_close($mbox); 
     } 
    } 

有什麼我可以改變上面?

回答

0

我不確定這是否是問題的原因,或者我是否正確理解了您的代碼。不過我認爲你的$ i = 0需要進入foreach循環,最後你需要失去++ $ i。

讓我們來看看它。首先,你設置$ i = 0。 foreach獲取第一條消息並進入for循環,循環遍歷$ i的遞增值。假設當for循環結束時$ i被設置爲4。在那一點上,++ $ i將它設置爲5. foreach迭代結束並且下一封電子郵件應該被處理。但是這並沒有發生,因爲$ i仍然是5,所以當你這樣做時:

$structure = imap_fetchstructure($mbox, $e[$i] , FT_UID) 

你正在選擇錯誤的電子郵件。

0

其實看着腳本,我認爲他在兩個地方使用$ i的方式不正確。 foreach中的$ i應該是一個不同的變量。

因爲他應該使用$我預期... 但郵件的部分應使用單獨的變量$結構變量(我用$ P)

我知道這是舊的文章,但它幫助了我一點。