【发布时间】:2017-07-27 23:29:54
【问题描述】:
我正在尝试编写一个脚本,从交换服务器下载电子邮件,然后将其插入数据库,但我无法以一种好的方式获取电子邮件的“文本部分”。
phpcode
<?PHP
$user = "email@domain.com";
$password = "password123";
$mbox = imap_open("{exchange01:993/imap/ssl/novalidate-cert}", $user, $password);
$message = imap_fetchbody($mbox,1,1);
print_r($message);
if($mbox)
{
imap_close($mbox);
};
?>
然后整个 html 正文被打印出来。我想这是可以预料的,但我不想拥有
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=iso-8859-1"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:"Neo Sans Std";}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
span.E-postmall17
..mumbojumbo,只是电子邮件中的文字(我可以忍受有签名和图像等等)。
有没有比粗略地将<body... 处的长字符串剪断到</body... 然后再从那里剪得更远的方法?一定有其他人想要解决同样的问题,但我花了一整天的时间试图解决它并用谷歌搜索它,却找不到任何答案。
我想最后我只是将整个 htmlresponse 插入到数据库单元格中并希望最好,但我宁愿不这样做。
帮助我,Stackoverflow。你是我唯一的希望
解决方案编辑:
不是我想要的确切解决方案,但它确实有效(需要稍作修复)。
echo strip_tags($message, '<body>');
输出只是
<body...>
Yayh the text i want!
</body .....>
部分。非常感谢@ThisGuyHasTwoThumbs(在 cmets 中)
编辑:
最后代码大致变成了这样
<?PHP
$user = "email@domain.com";
$password = "password";
$mbox = imap_open("{exchange01:993/imap/ssl/novalidate-cert}", $user, $password);
$message = imap_fetchbody($mbox,1,1);
$message = strip_tags($message, '<body>');
$message = explode(">", $message);
$message = explode("<", $message[1]);
$message = str_replace(" ", "", $message[0]);
$message = html_entity_decode($message);
$message = trim($message);
//Or the above three combined in one row
#$message = trim(html_entity_decode( str_replace(" ", "", $message[0])));
echo $message;
if($mbox)
{
imap_close($mbox);
};
?>
删除第一个<body something something something> 和末尾的</body>,然后删除变量开头和结尾的空格。 (@Goose 在下面他编辑的答案中也有点回答)。它还将 html 编码的 ' 字母转换为相应的字母,并删除   标记等。
【问题讨论】:
-
您不将正文内容(文本)与电子邮件代码分开存储吗?
-
@ThisGuyHasTwoThumbs 嗯?据我所知
imap_fetchbody($mbox,1,1);是下载邮件正文的正确方法,下载的内容是您在上面看到的(+更多 html 文本,然后最后是一些 YAYH 我想要的文本(还有一些 html 废话). -
我的意思是,如果你将 $body 存储在一个变量中,那么 标签之间的所有内容,那么你可以使用
strip_tags来摆脱其余部分 -
@ThisGuyHasTwoThumbs 哦..!正是我要找的,非常感谢!几个小时以来一直在为这个问题拉头发xD
-
:) 不用担心 :D 哈哈