Php 获取电子邮件imap_fetchbody()的正文
如何获取电子邮件正文 在这个示例中,只有大约25%的电子邮件正文被提取Php 获取电子邮件imap_fetchbody()的正文,php,imap,Php,Imap,如何获取电子邮件正文 在这个示例中,只有大约25%的电子邮件正文被提取 $structure = imap_fetchstructure($this->stream, $header->msgno); if(!empty($structure->parts[1])){ $message = imap_fetchbody($this->stream, $header->msgno, '1'); switch($structure->parts
$structure = imap_fetchstructure($this->stream, $header->msgno);
if(!empty($structure->parts[1])){
$message = imap_fetchbody($this->stream, $header->msgno, '1');
switch($structure->parts[1]->encoding){
case 1:
$message = imap_8bit($message);
break;
case 3:
$message = imap_base64($message);
break;
case 4:
$message = imap_qprint($message);
break;
}
echo $message;
}
更新
找到了一个解决方案:D
public function parse_mail($header){
$structure = imap_fetchstructure($this->stream, $header->msgno);
if(isset($structure->parts)){
$parse = $this->parse_parts($structure->parts, $header->msgno);
}
else{
$parse = $this->parse_parts($structure, $header->msgno);
}
if($parse){
print_r($parse);
}
}
private function parse_parts($parts, $msgno){
$message = imap_fetchbody($this->stream, $msgno, '1');
if(!empty($parts->encoding)){
switch($parts->encoding){
/*case 1:
$message = imap_8bit($message);
break;*/
case 3:
$message = imap_base64($message);
break;
case 4:
$message = imap_qprint($message);
break;
}
}
return $message;
}
“25%的电子邮件正文”?你收到了所有的信息,但每个信息只有1/4?或者,在16封邮件中,您只收到4封?请尝试使用$message=imap_fetchbody($mailbox,$email,1.2);去掉测试的其他内容将“1.1”更改为“1.2”,并尝试一下。您需要查看结构,找到text/html或text/plain部分。如果同时包含两个部分,则顶层(“1”)部分将是多部分/备选部分。如果它还有附件,顶层可能是多部分/相关的,带有多部分/备选子部分。
public function parse_mail($header){
$structure = imap_fetchstructure($this->stream, $header->msgno);
if(isset($structure->parts)){
$parse = $this->parse_parts($structure->parts, $header->msgno);
}
else{
$parse = $this->parse_parts($structure, $header->msgno);
}
if($parse){
print_r($parse);
}
}
private function parse_parts($parts, $msgno){
$message = imap_fetchbody($this->stream, $msgno, '1');
if(!empty($parts->encoding)){
switch($parts->encoding){
/*case 1:
$message = imap_8bit($message);
break;*/
case 3:
$message = imap_base64($message);
break;
case 4:
$message = imap_qprint($message);
break;
}
}
return $message;
}