2012-02-18 14 views
0

IMAP機能を使用して受信トレイから受信した添付ファイルを切り離し、指定したサーバーディレクトリに保存しようとしています。添付ファイルをデタッチしてディレクトリに保存するPHP IMAP機能

私はすべてのUNSEENメッセージでこれをやっていますが、問題は1つのメッセージだけです。

以下

がコードである(私は$ホスト、$ログイン、明白な理由のための$パスワード変数を削除):

$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 = 0を設定します。 foreachは最初のメッセージを取得し、$ iの値をインクリメントして繰り返すforループに入ります。 forループが終了したときに$ iが4に設定されているとします。その時点で、++ $ iは5に設定されます。foreachの繰り返しが終了し、次の電子メールが処理されます。

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

あなたは間違ったメールを選んでいる:$私はそうあなたがないとき、まだ5であるので、しかし、これは起こりません。

0

実際にスクリプトを見ると、彼は$ iを2か所で間違って使っていると思います。 foreach内の$ iは別の変数にする必要があります。予想通り、彼は$ Iを使用する必要があります$構造変数の

... が、メールの部分は別の変数を使用する必要があります(私は$ pを使用)

私はこれは古い記事ですけど、それ私を少し助けました。

関連する問題