Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从电子邮件PHP中提取正文文本_Php_Imap - Fatal编程技术网

从电子邮件PHP中提取正文文本

从电子邮件PHP中提取正文文本,php,imap,Php,Imap,我目前正在使用imap流从收件箱获取电子邮件 除了我不确定如何获取邮件的正文和标题外,一切正常。如果我做了imap\u body($connection,$message)文本中将包含电子邮件附件的基本64个等效项 我目前正在使用此功能获取附件 好吧,使用php imap函数并不有趣。此页面上的用户解释了与获取电子邮件不一致的原因: 利用他的有用信息,我创造了一种可靠的方法来获取电子邮件正文 $bodyText = imap_fetchbody($connection,$emailnumber,

我目前正在使用imap流从收件箱获取电子邮件

除了我不确定如何获取邮件的正文和标题外,一切正常。如果我做了
imap\u body($connection,$message)
文本中将包含电子邮件附件的基本64个等效项

我目前正在使用此功能获取附件


好吧,使用php imap函数并不有趣。此页面上的用户解释了与获取电子邮件不一致的原因:

利用他的有用信息,我创造了一种可靠的方法来获取电子邮件正文

$bodyText = imap_fetchbody($connection,$emailnumber,1.2);
if(!strlen($bodyText)>0){
    $bodyText = imap_fetchbody($connection,$emailnumber,1);
}
$subject = imap_headerinfo($connection,$i);
$subject = $subject->subject;

echo $subject."\n".$bodyText;
你也可以试试这些

content-type:text/html

$message = imap_fetchbody($inbox,$email_number, 2);

content-type:plaintext/text

$message = imap_fetchbody($inbox,$email_number, 1);
我的解决方案(适用于所有类型和字符集):

函数格式\u html($str){
//转换为完整的HTML格式和可转换的les代码ASCII 10 en$lf格式
$str=htmlentities($str,ENT_COMPAT,“UTF-8”);
$str=str_替换(chr(10),“
”,$str); 返回$str; } //开始 $obj_structure=imap_fetchstructure($imapLink,$obj_mail->msgno); //第二节内容包括信息和内容提取 $obj_段=$obj_结构; $section=“1”; 对于($i=0;$i<10;$i++){ 如果($obj_段->类型==0){ 打破 }否则{ $obj_section=$obj_section->parts[0]; $section.=($i>0?.1):“); } } $text=imap_fetchbody($imapLink,$obj_mail->msgno,$section); //Décodageéventuel 如果($obj_段->编码==3){ $text=imap_base64($text); }else if($obj_段->编码==4){ $text=imap\U qprint($text); } //文图埃尔酒店 foreach($obj_section->参数为$obj_param){ 如果($obj_参数->属性==“字符集”)和($obj_参数->值)!=“UTF-8”)){ $text=utf8\u编码($text); 打破 } } //结束 打印格式为html($text);
非常感谢。在谷歌搜索了4个小时后,我找到了一个有效的例子。那是关于什么的
1.2
?@pguardiario我不知道,不过如果你点击上面的链接,可能会有所帮助。我相信我通过反复试验找到了解决方案。@pguardiario您要检索的是多部分MIME文档的一部分。例如,text/plain、text/html……应接受此选项。适用于比当前接受答案更多类型的电子邮件。谢谢!
function format_html($str) {
    // Convertit tous les caractères éligibles en entités HTML en convertissant les codes ASCII 10 en $lf
    $str = htmlentities($str, ENT_COMPAT, "UTF-8");
    $str = str_replace(chr(10), "<br>", $str);
    return $str;
}


// Start

$obj_structure = imap_fetchstructure($imapLink, $obj_mail->msgno);

// Recherche de la section contenant le corps du message et extraction du contenu
$obj_section = $obj_structure;
$section = "1";
for ($i = 0 ; $i < 10 ; $i++) {
    if ($obj_section->type == 0) {
        break;
    } else {
        $obj_section = $obj_section->parts[0];
        $section.= ($i > 0 ? ".1" : "");
    }
}
$text = imap_fetchbody($imapLink, $obj_mail->msgno, $section);
// Décodage éventuel
if ($obj_section->encoding == 3) {
    $text = imap_base64($text);
} else if ($obj_section->encoding == 4) {
    $text = imap_qprint($text);
}
// Encodage éventuel
foreach ($obj_section->parameters as $obj_param) {
    if (($obj_param->attribute == "charset") && (mb_strtoupper($obj_param->value) != "UTF-8")) {
        $text = utf8_encode($text);
        break;
    }
}

// End
print format_html($text);