2012-02-18 92 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)

我知道这是旧的文章,但它帮助了我一点。