2015-03-25 46 views
0

我一直在嘗試獲取消息但未成功。PHP imap_fetchbody

$body = imap_fetchbody($inbox, $email_id, 0); 

沒有附件的消息是好的,我有輸出,但帶有附件 給出了一些複雜的輸出哪些HTML和純消息的一些(內容 - 類型)進行編碼,其是Gmail郵件

的一部分
+0

帶附件的消息通常以多部分形式出現,並用邊界字符串分隔。你究竟想要做什麼?你只是想獲得消息的文本部分? – vim 2015-03-25 13:05:44

+0

是的,我只需要短信 – 2015-03-26 05:44:58

回答

1

您可以使用下面的代碼來獲得一個多電子郵件的主體的純文本部分:

<?php 

//get the body 
$body = imap_fetchbody($inbox, $email_id, 0); 

//parse the boundary separator 
$matches = array(); 
preg_match('#Content-Type: multipart\/[^;]+;\s*boundary="([^"]+)"#i', $body, $matches); 
list(, $boundary) = $matches; 

$text = ''; 
if(!empty($boundary)) { 
    //split the body into the boundary parts 
    $emailSegments = explode('--' . $boundary, $body); 

    //get the plain text part 
    foreach($emailSegments as $segment) { 
     if(stristr($segment, 'Content-Type: text/plain') !== false) { 
      $text = trim(preg_replace('/Content-(Type|ID|Disposition|Transfer-Encoding):.*?\r\n/is', '', $segment)); 
      break; 
     } 
    } 
} 

echo $text; 
?> 
+0

我已經嘗試你的代碼,但列表(,$邊界)= $匹配;有未定義偏移的錯誤:1 – 2015-04-08 05:54:14

0

這有助於

$ body = imap_fetchbody($ inbox,$ email_id,1.1);

+0

這不適用於所有情況。 MIME非常靈活。 – Max 2015-03-26 15:19:18