我正在做的事情:
首先,我正在尝试获取电子邮件,并将用户的回复保存在我的数据库中。为此,我使用PHP imap
方法从我的电子邮件地址获取邮件。
代码:
function get_email()
{
//define server, email , password
$server = '{server.example.com:143}INBOX';
$user_name = 'login';
$user_pass = 'password';
$mail = imap_open( $server , $user_name , $user_pass );
$headers = imap_num_msg($mail);//counting the no of msg
for ($i = 1; $i <= $headers ; $i++)
{
$body = imap_fetchbody($mail, $i ,1);
$body = explode(">",$body);
$body = $body['0'];
$body = substr($body, 0, -5);
echo "<pre>";
echo "/-/-/-/-/-/".$body."/-/-/-/-/-/";
echo "</pre>";
}
// close the connection
imap_close($mail);
}
现在:我从$body = imap_fetchbody($mail, $i ,$i);
得到的结果是:
Okay What new in it.
On Thu, Feb 12, 2015 at 4:56 PM, admin wrote:
> My name is admin!
>
>
但只想要一个消息体CCD_ 3。所以我explode()
按摩,得到数组的第一个元素,去掉最后一行。
问题:我无法删除最后一行该程序的当前结果是:
Okay What new in it.
On Thu, Feb 12, 2015 at 4:56 PM, admin
即使我添加了"/-/-/-/-/-/"
符号,但该符号没有显示。
您的方法并不理想,这个答案也不理想。因为坦率地说,你试图完成的任务可能相当困难,因为电子邮件线程中以前的消息会因使用的客户端而异,你也不能信任用户("正文"下面的线程可能被用户更改等)
使用爆炸方式'>'并不理想,因为如果它是消息的一部分呢?
'n>
爆炸也是不可能的,因为用户可能会像@Michael_Berkovski所说的那样内联响应。
无论如何,这里是我的解决方案:
$body = preg_replace('#('n>.*?)+$#','',$body);
这将减少以下内容:
This is the body
> And this is a comment within the body
Also just the body
>Previous messages
>here
>> Even indented who cares?
至:
This is the body
> And this is a comment within the body
Also just the body
我非常努力地找到了一个解决方案,要求用户在按摩结束时用'#'
回复。正如我所注意到的,不同的邮件服务有不同类型的回复,但所有服务都会首先显示回复。
代码:
$msgno = imap_num_msg($mail);
for ($i = 1; $i <= $msgno ; $i++)
{
$body = imap_fetchbody($mail, $i ,1);
$body = explode("#",$body);
$body = $body['0'];
echo $body;
echo "<br>";
}
由此我得到了我渴望的结果。