2011-05-19 25 views
0

我想寫一個腳本,它將一個文件夾中的所有郵件下載到一個沒有自定義標記的文件夾中,我們現在稱之爲$ aNiceFlag標誌;在我提取郵件後,我想用$ aNiceFlag標記它。然而,在解決國旗問題之前,我有一個問題需要從郵件中提取我需要的內容。如何從zend郵件imap中提取內容?

這是我需要的信息:

  • 發件人(電子郵件地址和名稱,如果可能的話)
  • 主題
  • 接收機
  • 純文本正文(如果只有HTML可我會盡量把它轉換從HTML明文)
  • 時間發送

我可以通過使用$mailObject->subject輕鬆獲得該主題。我在看Zend Documentation,但對我來說有點困惑。

這裏是我的代碼,現在,我不應該呼應出的內容,但只是現在,而測試:

$this->gOauth = new GoogleOauth(); 
$this->gOauth->connect_imap(); 
$storage = new Zend_Mail_Storage_Imap(&$this->gOauth->gImap); 
$storage->selectFolder($this->label); 
foreach($storage as $mail){ 
    echo $mail->subject(); 
    echo strip_tags($mail->getContent()); 
} 

我訪問使用谷歌的OAuth郵件。 $this->label是我想要得到的文件夾。現在很簡單,但在使其變得複雜之前,我想弄清楚基本原理,比如將所有上面列出的數據提取到數組中的單獨鍵中的合適方法。

回答

4

你可以很容易地使用你所使用的主題相同的技術獲得發件人,收件人和日期的頭,但實際的明文身體是比較麻煩一些,這裏是一個示例代碼,會做你想要

什麼
$this->gOauth = new GoogleOauth(); 
    $this->gOauth->connect_imap(); 
    $storage = new Zend_Mail_Storage_Imap(&$this->gOauth->gImap); 
    $storage->selectFolder($this->label); 
    // output first text/plain part 
    $foundPart = null; 
    foreach($storage as $mail){ 
     echo '----------------------<br />'."\n"; 
     echo "From: ".utf8_encode($mail->from)."<br />\n"; 
     echo "To: ".utf8_encode(htmlentities($mail->to))."<br />\n"; 
     echo "Time: ".utf8_encode(htmlentities(date("Y-m-d H:s" ,strtotime($mail->date))))."<br />\n"; 
     echo "Subject: ".utf8_encode($mail->subject)."<br />\n"; 

     foreach (new RecursiveIteratorIterator($mail) as $part) { 
      try { 
       if (strtok($part->contentType, ';') == 'text/plain') { 
        $foundPart = $part; 
        break; 
       } 
      } catch (Zend_Mail_Exception $e) { 
       // ignore 
      } 
     } 
     if (!$foundPart) { 
      echo "no plain text part found <br /><br /><br /><br />\n\n\n"; 
     } else { 
      echo "plain text part: <br />" . 
        str_replace("\n", "\n<br />", trim(utf8_encode(quoted_printable_decode(strip_tags($foundPart))))) 
       ." <br /><br /><br /><br />\n\n\n"; 
     } 
    }