2012-09-28 65 views
1

下面我通过电子邮件循环,使用PHP的imap库。 它的工作原理,但我坚持保存消息的附件。PHP imap下载附件

<?php 

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; 
$username = '[email protected]'; 
$password = 'somepassword'; 

/* try to connect */ 
$imap_connection = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); 

/* grab emails */ 
$emails = imap_search($imap_connection,'ALL'); 

/* if emails are returned, cycle through each... */ 
if($emails) { 

    /* begin output var */ 
    $output = ''; 

    /* put the newest emails on top */ 
    rsort($emails); 

    /* for every email... */ 
    foreach($emails as $email_number) { 

     $structure = imap_fetchstructure($imap_connection,$email_number); 
     if(isset($structure->parts) && count($structure->parts)){ 

      for($i = 0; $i < count($structure->parts); $i++){ 


       if(!empty($structure->parts[$i]->bytes)){ 

        if(!empty($ifdparameters)){ 
         echo "File: ----".$structure->parts[$i]->dparameters[0]->value; 
              // returns: testMeOut.jpg 

        } 
       } 


      } //eof $i loop 


     } //eof $structure->parts 


      } // eof foreach $emails 

} // eof if($emails) 

?> 

我想了解如何将附件保存到我的服务器上的目录。 我知道:$ structure-> parts [$ i] - > dparameters [0] - > value;正在返回附件的名称。

我敢肯定我需要用imap_fetchbody()来做些什么,但是我非常困惑,阅读在线教程和php的网站。

任何人都可以看到我失踪的最后几步如何从$消息保存附件?

回答

6

一旦写入下载Excel文件,

猜猜它可以改变几乎任何电子邮件附件做明智的。

基本上,它表明你缺少两个步骤:

$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"; 

    function get_decode_value($message, $encoding) { 
     switch($encoding) { 
      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; 
    } 

    for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++) { 

     $structure = imap_fetchstructure($mbox, $jk , FT_UID); 

     /* retrieve message timestamp */ 
     $header = imap_headerinfo($mbox, $jk); 
     $timestamp = $header->udate; 

     $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; 
       $body="";$data=""; 

       if($filename=='SomeFile.XLS'){ 
        $body = imap_fetchbody($mbox, $jk, $fpos); 
        $dst = '/SomeLocalPath/SomeOtherFile.xls'; 

        /* don't overwrite */ 
        if(!file_exists($dst)){ 
         $fp = fopen($dst, 'w'); 
         $data = get_decode_value($body, $part->type); 
         fputs($fp, $data); 
         fclose($fp); 
        } 
       } 
       $fpos++; 
      } 
     } 
    } 

我宁愿不知道为什么附件无法通过任何谷歌API访问...

为什么没有双向方向GMail API,如单向Mandrill API?