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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4272551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:29:44  来源:igfitidea点击:

Extract body text from Email PHP

phpimap

提问by Lime

I am currently using an imap stream to get emails from an inbox.

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

Everything is working fine except I am unsure how to get the body text and title of the email. If I do imap_body($connection,$message)the base 64 equivalent of the email attachment is included in the text.

一切正常,除了我不确定如何获取电子邮件的正文和标题。如果我这样做imap_body($connection,$message),则文本中包含电子邮件附件的 base 64 等效项。

I am currently using this function to get the attachments.

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

http://www.electrictoolbox.com/function-extract-email-attachments-php-imap/

http://www.electrictoolbox.com/function-extract-email-attachments-php-imap/

回答by Lime

Well php imap's function are not fun to work with. A user on this page explains the inconsistencies with getting emails: http://php.net/manual/en/function.imap-fetchbody.php#89002

好吧,php imap 的功能使用起来并不有趣。此页面上的用户解释了接收电子邮件的不一致之处:http: //php.net/manual/en/function.imap-fetchbody.php#89002

Using his helpful information I created a reliably way to get an email's body text.

使用他的有用信息,我创建了一种可靠的方式来获取电子邮件的正文。

$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;

回答by Jerry

My solution (works with all types and charset) :

我的解决方案(适用于所有类型和字符集):

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);

回答by Kailas

You Can also try these

你也可以试试这些

content-type:text/html

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

content-type:plaintext/text

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