2013-02-17 42 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

相關問題