tim*_*son 7 php regex email parsing sendgrid
我正在使用Sendgrid和他们的Parse API来发送/接收电子邮件.Parse API允许一个人的网络应用程序接收电子邮件作为$ _POST,但问题是在$ _POST中我希望能够从其先前的消息和链接的元数据中提取消息本身.
为了向您展示我在下图中的含义,我只想捕捉文本"尝试从GMAIL从12373发送到12373"而不是它下面的所有垃圾.如果这是不可能的,有没有人有任何关于如何解析电子邮件正文($_POST['text'])的建议,以便我可以分离出消息本身?
问题是根据电子邮件客户端(gmail,outlook等),我不清楚日期信息,在这种情况下:"2013年1月23日星期三......",将允许关注消息本身.如果所有电子邮件客户端都将日期放在消息下面,那么看起来我可以设计一个花哨的正则表达式来查找换行符后跟日期或其他内容.思考?

Swi*_*ift 14
你有几个选择:
1)插入分割电子邮件的令牌
你可以做类似的事情--- reply above this line ---,然后删除该令牌下方的所有内容.
2)使用电子邮件回复解析库
github有一个非常好的,但它是红宝石.虽然有一个php端口可能对你需要的东西有益:
完全工作的代码:
<?php
require_once 'application/third_party/EmailReplyParser-master/src/autoload.php';
$email = new \EmailReplyParser\Email();
$reply = $email->read($_POST['text']);
$message=$reply[0]->getContent();
$message=preg_replace('~On(.*?)wrote:(.*?)$~si', '', $message);
//Last line is needed for some email clients, e.g., some university e-mails: foo@bar.edu but not Gmail or Hotmail, to get rid of "On Jan 23...wrote:"
//This failure to remove "On Jan 23...wrote:" is a known issue and is documented in their README
?>
Run Code Online (Sandbox Code Playgroud)