2013-04-24 110 views
1

我試圖從一個XML文件中獲取信息,這個XML文件是作爲附件發送給一封電子郵件的。我使用mime_parser.php來解析電子郵件。這裏是我正在使用的代碼:使用php獲取附件信息mime_parser

$email = ""; 
while (!feof($fd)) { 
$email .= fread($fd, 1024); 
} 
fclose($fd); 

//SET UP EMAIL PARSER CLASS 
$mime=new mime_parser_class; 
$mime->ignore_syntax_errors = 1; 
$parameters=array('Data'=>$email,); 

//RESULTS GET ADDED TO $decoded VARIABLE 
$mime->Decode($parameters, $decoded); 

//---------------------- GET EMAIL HEADER INFO -----------------------// 

//get the name and email of the sender 
$fromName = $decoded[0]['ExtractedAddresses']['from:'][0]['name']; 
$fromEmail = $decoded[0]['ExtractedAddresses']['from:'][0]['address']; 

//get the name and email of the recipient 
$toEmail = $decoded[0]['ExtractedAddresses']['to:'][0]['address']; 
$toName = $decoded[0]['ExtractedAddresses']['to:'][0]['name']; 

//get the subject 
$subject = $decoded[0]['Headers']['subject:']; 

$removeChars = array('<','>'); 

//get the message id 
$messageID = str_replace($removeChars,'',$decoded[0]['Headers']['message-id:']); 

//get the reply id 
$replyToID = str_replace($removeChars,'',$decoded[0]['Headers']['in-reply-to:']); 
//---------------------- END GET EMAIL HEADER INFO -----------------------// 

//---------------------- FIND THE BODY -----------------------// 
//get the message body 
if(substr($decoded[0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Body'])){ 
    $body = $decoded[0]['Body']; 
} elseif(substr($decoded[0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Body'])) { 
    $body = $decoded[0]['Parts'][0]['Body']; 
} elseif(substr($decoded[0]['Parts'][0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Parts'][0]['Body'])) { 
    $body = $decoded[0]['Parts'][0]['Parts'][0]['Body']; 
} 
//---------------------- END FIND THE BODY -----------------------// 

我找不到任何關於如何獲取附件信息。一些例子表明它是標題的一部分,但據我所知它不是。如果有人有關於附件信息的信息,我會很感激。

回答

0

你可以試試這個PHP Mime Mail Parser link

實施例中列出:

<?php 
require_once('MimeMailParser.class.php'); 

$path = 'path/to/mail.txt'; 
$Parser = new MimeMailParser(); 
$Parser->setPath($path); 

$to = $Parser->getHeader('to'); 
$from = $Parser->getHeader('from'); 
$subject = $Parser->getHeader('subject'); 
$text = $Parser->getMessageBody('text'); 
$html = $Parser->getMessageBody('html'); 
$attachments = $Parser->getAttachments();  
?>