2011-01-30 43 views
1

Zend Framework中是否有任何類允許我輕鬆地閱讀電子郵件?Zend Mail和編碼,內容傳輸等 - 統一?

Zend_Mail類允許我輕鬆獲取標題,主題和內容正文。但是將所有內容轉換爲UTF-8格式和可讀格式仍然很痛苦。

或者我做錯了什麼?據我所知,Zend Framework不允許我輕鬆獲得我可以使用的UTF-8字符串,但我仍然需要做一些後期處理。對?

+0

在2010年初我做了一些實驗與Zend郵件POP3類,它是很容易,我能真正的郵件內容,主題和形式,我只用英文進行測試,可否請您舉一個關於編碼問題的例子...... – tawfekov 2011-01-30 17:48:40

回答

0

關鍵是你需要迭代消息中的部分並找到文本。一旦你有了它,那麼你可以使用quoted_printable_decode以有用的方式獲取文本本身。

這是一些粗略的代碼,使用Zend_Mail讀取IMAP電子郵件信箱:

<?php 
$mail = new Zend_Mail_Storage_Imap(array(
     'host' => EMAIL_ACCOUNT_HOST, 
     'user'  => EMAIL_ACCOUNT_USERNAME, 
     'password' => EMAIL_ACCOUNT_PASSWORD, 
    )); 

echo (int)$mail->countMessages() . " messages found\n"; 

foreach ($mail as $message) { 

    $from = $message->getHeader('from'); 
    $subject = trim($message->subject); 
    $to = trim($message->to); 
    $body = getBody($message); 

    // do something with message here 
} 

function getBody(Zend_Mail_Message $message) 
{ 
    // find body 
    $part = $message; 
    $isText = true; 
    while ($part->isMultipart()) { 
     $foundPart = false; 
     $iterator = new RecursiveIteratorIterator($message); 
     foreach ($iterator as $part) { 
      // this detection code is a bit rough and ready! 
      if (!$foundPart) { 
       if (strtok($part->contentType, ';') == 'text/html') { 
        $foundPart = $part; 
        $isText = false; 
        break; 
       } else if (strtok($part->contentType, ';') == 'text/plain') { 
        $foundPart = $part; 
        $isText = true; 
        break; 
       } 
      } 
     } 

     if($foundPart) { 
      $part = $foundPart; 
      break; 
     } 
    } 
    $body = quoted_printable_decode($part->getContent()); 

}