2011-11-07 139 views
1

如何在电子邮件中获取附件?获取附件 - imap_fetchbody()

代码

$structure = imap_fetchstructure($this->stream, $this->msgno); 
//print_r($structure); 
if(isset($structure->parts)){ 
    foreach($structure->parts as $section => $part){ 
     if(isset($part->disposition)){ 
      if(strtolower($part->disposition) == 'attachment'){ 
       echo 'section = '.$section."\n"; 
       $body = imap_fetchbody($this->stream, $this->msgno, $section); 

       $finfo = new finfo(FILEINFO_MIME); 
       echo $finfo->buffer($body) . "\n"; 

       $file = Init::$dynamic['data_path'].$this->msgno.'_'.$part->dparameters[0]->value; 

       imap_savebody($this->stream, $file, $this->msgno, $section); 

       echo $finfo->file($file) . "\n"; 

       //print_r($body); 
      } 
     } 
    } 
} 

返回的附件甚至没有接近正确的文件大小和返回的数据看起来是这样

ZHUga2VuZGVyIHZlbCBpa2tlIG5vZ2VuIHNvbSBlciB2ZWQgYXQgc3RhcnRlIGVnZW4gdmly 
a3NvbWhlZCBlbGxlciBzb20gbGlnZSBlciBzdGFydGV0IG9wPyA6KQ0KDQpmb3JkaSBqZWcg 
bmV0b3AgZXIgYmxldmV0IGbmcmRpZyBtZWQgYXQgbGF2ZSBldCD4a29ub21pc3lzdGVtIG1l 
ZCBlbiByZXZpc29yIHNvbSBlciB1ZHZpa2xldCBtZWQgc+ZybGlndCBoZW5ibGlrIHDlIGl2 
5nJrc+Z0dGVyZSBzb20gb2Z0ZSBpa2tlIGhhciBkZW4gc3RvcmUgaW5kc2lndCBpIPhrb25v 
bWkgOik= 

相同文件要么具有相同的文件大小,即使他们是副本

+1

http://php.net/manual/en/function.imap-base64.php可能会帮助 –

回答

4

我认为你让你的代码复杂化。

你的问题是你没有得到你的数据进行编码

$body = imap_fetchbody($this->stream, $this->msgno, $part); 
//$part=2 (first attachment file) 
//$part=3 (second attachment file) ... 

,然后才能在该文件中得到你的实际数据请看看

$coding = $structure->parts[$part]->encoding; 
if ($coding == 0) { 
    $body = imap_7bit($body); 
} elseif ($coding == 1) { 
    $body= imap_8bit($body); 
} elseif ($coding == 2) { 
    $body = imap_binary($body); 
} elseif ($coding == 3) { 
    $body = imap_base64($body); 
} elseif ($coding == 4) { 
    $body = imap_qprint($body); 
} elseif ($coding == 5) { 
    $body = $body; 
} 

所以你可以尝试把它打印出来out:

echo $body; 

注意:$ section是文件的一部分。

1

你有什么是二进制数据的ASCII表示,使用编码方案base64编码。一旦收到完整的字符串,使用base64_decode即可访问二进制数据,通常由尾部的=表示。

0
$emailtop= imap_search($inbox,'SUBJECT "'.$word.'" SINCE "'.sinceDate.'" BEFORE "'.beforeDate.'"'); 



/* if any emails found, iterate through each email */ 
if($emailtop) { 

    /* put the newest emails on top */ 
    //rsort($emails); 
    //print_r($emails); //die; 
    /* for every email... */ 
    foreach($emailtop as $email_number) 
    { 
     echo "</br> emailtop".$email_number; 

     $overview = imap_fetch_overview($inbox,$email_number,0); 
     echo '</br><span class="subject">'.$overview[0]->subject.'</span> '; 
     //echo 'ds</br><span class="subject">'.$overview[0]->subject.'</span> '; 
     if (!file_exists($dateTime)) { 
      mkdir($dateTime, 0777, true); 
     } 

     /* get mail structure */ 
     $structure = imap_fetchstructure($inbox, $email_number); 

     $attachments = array(); 

     /* if any attachments found... */ 
     if(isset($structure->parts) && count($structure->parts)) 
     { 
      echo "<pre>";print_r($structure->parts); 

      for($i = 0; $i < count($structure->parts); $i++) 
      { 
       if(isset($structure->parts[$i]->parts) && !empty($structure->parts[$i]->parts) && count($structure->parts[$i]->parts)>0) 
       { 
        echo "</br> SubCategory".$email_number; 
        //echo "<pre>";print_r($structure->parts[$i]); 
        for($y = 0; $y < count($structure->parts[$i]->parts); $y++) 
        { 
         echo "</br> SubCategory loop".$email_number; 
         //echo "<pre>";print_r($structure->parts[$i]->parts); 
         $attachments[$y] = array(
         'is_attachment' => false, 
         'filename' => '', 
         'name' => '', 
         'attachment' => '' 
        ); 

        //die; 

        if($structure->parts[$i]->parts[$y]->ifdparameters==1) 
        { 


         foreach($structure->parts[$i]->parts[$y]->dparameters as $object) 
         { 

          if(strtolower($object->attribute) == 'filename') 
          { 
           $attachments[$y]['is_attachment'] = true; 
           $attachments[$y]['filename'] = $object->value; 
          } 
         } 
        } 

        if($structure->parts[$i]->parts[$y]->ifparameters) 
        { 
         foreach($structure->parts[$i]->parts[$y]->parameters as $object) 
         { 
          if(strtolower($object->attribute) == 'name') 
          { 
           $attachments[$y]['is_attachment'] = true; 
           $attachments[$y]['name'] = $object->value; 
          } 
         } 
        } 

        if($attachments[$y]['is_attachment']) 
        { 
         $attachments[$y]['attachment'] = imap_fetchbody($inbox, $email_number, ($i+1).'.'.($y+1)); 

         /* 4 = QUOTED-PRINTABLE encoding */ 
         if($structure->parts[$i]->parts[$y]->encoding == 3) 
         { 
          //echo "hi ". $attachments[$y]['attachment']; die; 

          $attachments[$y]['attachment'] = base64_decode($attachments[$y]['attachment']); 
         } 
         /* 3 = BASE64 encoding */ 
         elseif($structure->parts[$i]->parts[$y]->encoding == 4) 
         { 

          $attachments[$y]['attachment'] = quoted_printable_decode($attachments[$y]['attachment']); 
         } 

        } 
        } 
       } 
       else 
       { 
        echo "</br> Single".$email_number; 
        $attachments[$i] = array(
         'is_attachment' => false, 
         'filename' => '', 
         'name' => '', 
         'attachment' => '' 
        ); 

        //die; 
        if($structure->parts[$i]->ifdparameters==1) 
        { 


         foreach($structure->parts[$i]->dparameters as $object) 
         { 


          if(strtolower($object->attribute) == 'filename') 
          { 
           $attachments[$i]['is_attachment'] = true; 
           $attachments[$i]['filename'] = $object->value; 
          } 
         } 
        } 

        if($structure->parts[$i]->ifparameters) 
        { 
         foreach($structure->parts[$i]->parameters as $object) 
         { 
          if(strtolower($object->attribute) == 'name') 
          { 
           $attachments[$i]['is_attachment'] = true; 
           $attachments[$i]['name'] = $object->value; 
          } 
         } 
        } 

        if($attachments[$i]['is_attachment']) 
        { 
         $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1); 


         /* 4 = QUOTED-PRINTABLE encoding */ 
         if($structure->parts[$i]->encoding == 3) 
         { 
          $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); 
         } 
         /* 3 = BASE64 encoding */ 
         elseif($structure->parts[$i]->encoding == 4) 
         { 
          $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); 
         } 
        } 
       } 
      } 
     } 

     /* iterate through each attachment and save it */ 
     foreach($attachments as $attachment) 
     { 

      if($attachment['is_attachment'] == 1) 
      { 

        $filename = $attachment['name']; 
        if(empty($filename)) 
        $filename = $attachment['filename']; 

       if(!in_array($filename, $dowloadAttachments)) 
       { 
        $dowloadAttachments[] = $filename; 

        /* To check extension of attachment */ 
        $info = pathinfo($filename); 

        $xlsExtension = array('xls','xlsm','xlss','csv'); 
        if (in_array($info["extension"],$xlsExtension)) { 




         /* prefix the email number to the filename in case two emails 
         * have the attachment with the same file name. 
         */ 
         $tmpFile = $filename; 
         $filename = $dateTime."/".$filename; 
         $fp = fopen($filename, "w+"); 
         fwrite($fp, $attachment['attachment']); 
         fclose($fp); 
         require "calculateValue.php"; 



} 
       } 
      } 
+2

欢迎使用Stack Overflow!虽然代码非常值得赞赏,并有助于说明解决方案,但如果您描述您的方法并说明不同方法的不同机会,优点或缺点,这也会有所帮助。 – Lepidopteron