2013-02-01 102 views
1

我的電子郵件被傳送到我的Zend Framework 2索引(遵循MVC),並被髮送到我的控制器。如何用Zend Framework 2解析原始電子郵件?

public function incomingMailAction() 
{ 
    $message =''; 
    $stdin = fopen('php://stdin', 'r'); 

    while($line = fgets($stdin)) { 
     $message .= $line; 
    } 

    fclose($stdin); 

    // Parse e-mail here and store in database (including attachments) 
} 

我可以處理在數據庫中的部分存儲,我只是不知道如何拍攝的原始郵件,然後把它變成有用的東西(收件人,發件人的ReplyTo,CC,BCC,接頭,附件...等)。

謝謝!

回答

3

您可以使用Zend\Mail\Message::fromString($rawMessage);它雖然不會解碼MIME體。

+2

我該如何解碼?並獲得附件? – Josh

-1
public function incomingMailAction() 
{ 
    $message =''; 
    $stdin = fopen('php://stdin', 'r'); 

    while($line = fgets($stdin)) { 
     $email .= $line; 
    }  

    fclose($stdin); 

    $to1 = explode ("\nTo: ", $email); 
    $to2 = explode ("\n", $to1[1]); 
    $to = str_replace ('>', '', str_replace('<', '', $to2[0])); 
    list($toa, $tob) = explode('@', $to); 
} 

被盜來源:PHP email Piping get 'to' field

+0

我正在尋找類似*的東西,但使用Zend 2 Framework。 – Josh

1

我也嘗試用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擴展。

+0

這似乎是目前最好的解決方案。嘗試使用Zend1並最終使用這種方法。優秀! – andreaslangsays

相關問題