2015-03-25 48 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