我也嘗試用ZF2解析電子郵件,但是我實際上在Zend Mail組件的源代碼中發現了一條評論,解碼消息在待辦事項列表中,但尚未實現。目前似乎沒有簡單的方法來做到這一點。
相反,我建議使用php-mime-mail-parser - 我最終使用該庫代替。它使用pecl擴展mailparse(您可能需要安裝)的功能,並且非常容易。一些例子應該讓你開始:
$message = new \PhpMimeMailParser\Parser();
$message->setText($rawMail); // Other functions to set a filename exists too
// All headers are retrieved in lowercase, "To" becomes "to"
// and "X-Mailer" becomes "x-mailer"
$recipient = $message->getHeader('to');
$date = $message->getHeader('date');
$xmailer = $message->getHeader('x-mailer');
// All headers can be retrieved at once as a simple array
$headers = $message->getHeaders();
$recipient = $headers['to'];
// Attachments can be retrieved all at once as "Attachment" objects
$attachments = $message->getAttachments();
foreach($attachments as $attachment) {
$attachment_as_array = array(
'type' => $attachment->getContentType(),
'name' => $attachment->getFilename(),
'content' => (string)$attachment->getContent(),
);
}
因爲庫使用PHP的現有擴展,似乎是在內存管理方面非常有效它可能是一個更適合解析電子郵件比ZF將永遠是 - 而且它也很容易使用。我唯一的缺點是在每臺服務器上額外安裝了mailparse pecl擴展。
我該如何解碼?並獲得附件? – Josh