gpt4 book ai didi

php - 从 imap_open 修剪在电子邮件正文中添加的额外换行符

转载 作者:行者123 更新时间:2023-12-03 11:22:54 27 4
gpt4 key购买 nike

我正在使用以下函数来检索电子邮件正文,同时使用 PHP 的 imap_open功能。

它只是由 $email_body = getBody($email_number, $inbox); 调用

它工作得很好,但我有时遇到的一个问题是带有换行符的电子邮件最终会添加额外的换行符。

我可以尝试添加额外的换行符吗?

function getBody($uid, $imap) {
$body = get_part($imap, $uid, "TEXT/HTML");
// if HTML body is empty, try getting text body
if ($body == "") {
$body = get_part($imap, $uid, "TEXT/PLAIN");
}
return nl2br($body);
}

function get_part($imap, $uid, $mimetype, $structure = false, $partNumber = false) {
if (!$structure) {
//$structure = imap_fetchstructure($imap, $uid, FT_UID);
$structure = imap_fetchstructure($imap, $uid);
}
//var_dump($structure);
if ($structure) {
if ($mimetype == get_mime_type($structure)) {
if (!$partNumber) {
$partNumber = 1;
}
//$text = imap_fetchbody($imap, $uid, $partNumber, FT_UID);
$text = imap_fetchbody($imap, $uid, $partNumber);
switch ($structure->encoding) {
case 3: return imap_base64($text);
case 4: return imap_qprint($text);
default: return $text;
}
}

// multipart
if ($structure->type == 1) {
foreach ($structure->parts as $index => $subStruct) {
$prefix = "";
if ($partNumber) {
$prefix = $partNumber . ".";
}
$data = get_part($imap, $uid, $mimetype, $subStruct, $prefix . ($index + 1));
if ($data) {
return $data;
}
}
}
}
return false;
}

function get_mime_type($structure) {
$primaryMimetype = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");

if ($structure->subtype) {
return $primaryMimetype[(int)$structure->type] . "/" . $structure->subtype;
}
return "TEXT/PLAIN";
}

最佳答案

您可以使用 preg_replace 删除所有额外的新行.

preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $your_email_body);

关于php - 从 imap_open 修剪在电子邮件正文中添加的额外换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61089338/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com