2013-02-17 46 views
0

我一直坚持了几个小时。但基本上,我有一个收件箱,可以发送带有一些我想要取出的内容的电子邮件,以及附带的PDF。我想出了如何将所有HTML都取出来,但我如何保存附加的PDF?如何从电子邮件中获得附加的PDF并保存它,php

这是我鳕迄今为止:

公共函数read_emails(){// 设置用户检查

$strUser  = "email"; 
    $strPassword = "passwrd"; 
    $delete_emails=false; 

    // open 
    $hMail = imap_open ("{webmail.somethingsomething.com:143/notls}INBOX", "$strUser", "$strPassword") or die("can't connect: " . imap_last_error()); 
    // get headers 
    $aHeaders = imap_headers($hMail); 
    // get message count 
    $objMail = imap_mailboxmsginfo($hMail); 
    if($objMail != NULL) 
    {   
     // process messages 
     for($idxMsg = 1; $idxMsg <= $objMail->Nmsgs; $idxMsg++ ) 
     { 
      // get header info 
      $objHeader = imap_headerinfo($hMail, $idxMsg); 

      // get from object array 
      $aFrom = $objHeader->from; 

      // process headers 
      for($idx = 0; $idx < count($aFrom); $idx++) 
      { 
       // get object 
       $objData = $aFrom[ $idx ]; 
       // get email from 
       $strEmailFrom = $objData->mailbox . "@" . $objData->host; 
       //Get the subject 
       $message_subject = $objHeader->subject; 
       //Get the body of the message 
       $fullBody = imap_fetchbody($hMail,$idxMsg, 2);//displays full body including junk 
       $bodyMessage = quoted_printable_decode($fullBody); 
       //Get the msg structure 
       $message_structure = imap_fetchstructure($hMail, $idx); 
       //WHAT DO I DO HERE???? 
      } 

      // delete message 
      if($delete_emails == true){ 
       imap_delete($hMail, $idxMsg); 
      } 

     } 

     // expunge deleted messages 
     if($delete_emails == true){ 
      imap_expunge($hMail); 
     } 


     //Clears the cache 
     imap_gc($hMail, IMAP_GC_ELT); 

    } 
    // close 
    imap_close($hMail); 



} 

我有消息结构,如果我的print_r,我可以看到其中一个部分有附件,我可以看到它是PDF。不知道接下来会发生什么。我试过了,似乎无法找到一个好的解决方案。

谢谢!

编辑:这是imap_fetchstructure

stdClass的对象([型] => 1 [编码] => 0 [ifsubtype] => 1 [亚型] =>混合[ifdescription] => 0 [内容ifid] => 0 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => Array([0] => stdClass Object([attribute] => boundary [value] => f46d044473ef81609f04d5f1997c )[[]] =>数组[= 0] => [数据类型] => [数据类型] => [数据类型] => [数据类型] => 0 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => Array([0] => stdClass Object([attribute] => boundary [value] => f46d044473ef81609b04d5f1997a)) [parts] => Array([0] => stdClass Object([type] => 0 [encoding] => 0 [ifsubtype] => 1 [subtype] => PLAIN [ifdescription] => 0 [ifid] => 0 [lines] => 1 [bytes] => 37 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] =>参数] =>数组([0] => stdClass Object([attribute] => charset [value] => ISO-8859-1))[1] => stdClass Object([type] => 0 [encoding] => 0 [ifsubtype] => 1 [subtype] => HTML [ifdescription] => 0 [ifid] => 0 [lines] => 1 [bytes] => 37 [ifdisposition] => 0 [ifdparameters] => )[0] => stdClass Object([attribute] => charset [value] => ISO-8859-1)))))[1] => stdClass对象([type] => 3 [encoding] => 3 [ifsubtype] => 1 [subtype] => PDF [ifdescription] => 0 [ifid] => 0 [bytes] => 179876 [ifdisposition] =>处理] =>附件[ifdparameters] => 1 [dparameters] => Array([0] => stdClass Object([attribute] => filename [value] => Statement of Account.pdf))[ifparameters] => 1 [参数] =>数组([0] => stdClass对象([属性] =>名称[值] =>帐户报表。 PDF)))))

回答

0

按照doc,你的身体部位的数组中$message_structure->parts,所以你可以遍历这个数组,并停止在部分的->type->subtype对应PDF文档。就像:

foreach ($message_structure->parts as $idx => $part) { 
    if (TYPEAPPLICATION === $part->type && 'pdf' === strtolower($part->subtype)) { 
     $data = imap_fetchbody($hMail, $idxMsg, $idx); 
     // Then you should check the $part->encoding 
     // to decode data appropriately, e.g.: 
     if (ENCBASE64 === $part->encoding) { 
      $data = base64_decode($data); 
     } 
    } 
} 
+0

难道你不需要下载pdf或其他东西并保存它?你如何做到这一点 – Bill 2013-02-17 22:17:55

+0

我想我会得到的数据,并以某种方式编码? – Bill 2013-02-17 22:21:17

+0

当然,看到更新的答案 – lazyhammer 2013-02-17 22:33:32

相关问题