解析电子邮件,仅在从 Mandrill Inbound 返回时回复 API

Parsing e-mail, only reply, when returned from Mandrill Inbound API

使用 Mandrill 的 API 及其入站路由功能,我正在尝试 接收来自电子邮件的回复。我正在尝试只解析最新的回复,类似于 Asana 或 Zendesk 允许您回复电子邮件的方式,它最终会出现在他们的数据库中(而不是使用他们的网络应用程序)。

这是 Mandrill returns:

的 JSON 有效负载
[
    {
        "event": "inbound",
        "ts": 1421946625,
        "msg": {
            "raw_msg": "Received: from server (unknown [ip])\n\tby ip-ip (Postfix) with ESMTPS id 0F62820519\n\tfor <from@mail.mydomain.com>; Thu, 22 Jan 2015 17:10:24 +0000 (UTC)\nReceived: from server ([ip]) by\n server ([ip]) with mapi id\n ip; Thu, 22 Jan 2015 09:10:23 -0800\nFrom: person<person@mydomain.com>\nTo: Person\n\t<person@mail.mydomain.com>\nSubject: test\nThread-Topic: test\nThread-Index: AQHQNmZOwbsfpy7UQUuXQM6h0Jn9Gw==\nDate: Thu, 22 Jan 2015 17:10:22 +0000\nMessage-ID: <message@mydomain.com>\nReferences: <id@api.mydomain.com>\nIn-Reply-To: <id@api.mydomain.com>\nAccept-Language: en-US\nContent-Language: en-US\nX-MS-Has-Attach:\nX-MS-TNEF-Correlator:\nx-originating-ip: [myip]\nContent-Type: text/plain; charset=\"us-ascii\"\nContent-ID: <id@mydomain.com>\nContent-Transfer-Encoding: quoted-printable\nMIME-Version: 1.0\n\ntest",
            "headers": {
                "Received": [
                    "from server (unknown [ip]) by ip (Postfix) with ESMTPS id id for <person@mail.mydomain.com>; Thu, 22 Jan 2015 17:10:24 +0000 (UTC)",
                    "from server ([ip]) by server ([ip]) with mapi id id; Thu, 22 Jan 2015 09:10:23 -0800"
                ],
                "From": "Person Person <person@mydomain.com>",
                "To": "Person Person <person@mail.mydomain.com>",
                "Subject": "test",
                "Thread-Topic": "test",
                "Thread-Index": "id==",
                "Date": "Thu, 22 Jan 2015 17:10:22 +0000",
                "Message-Id": "<id@mydomain.com>",
                "References": "<id@api.mydomain.com>",
                "In-Reply-To": "<id@api.mydomain.com>",
                "Accept-Language": "en-US",
                "Content-Language": "en-US",
                "X-Ms-Has-Attach": "",
                "X-Ms-Tnef-Correlator": "",
                "X-Originating-Ip": "[ip]",
                "Content-Type": "text/plain; charset=\"us-ascii\"",
                "Content-Id": "<id@server>",
                "Content-Transfer-Encoding": "quoted-printable",
                "Mime-Version": "1.0"
            },
            "text": "test",
            "text_flowed": false,
            "from_email": "person@mydomain.com",
            "from_name": "Person Person",
            "to": [
                [
                    "person@mail.mydomain.com",
                    "Person Person"
                ]
            ],
            "subject": "test",
            "spf": {
                "result": "permerror",
                "detail": "SPF Permanent Error: No valid SPF record for included domain: _spf.intermedia.net: include:_spf.intermedia.net"
            },
            "spam_report": {
                "score": 0.6,
                "matched_rules": [
                    {
                        "name": "RCVD_IN_DNSWL_LOW",
                        "score": -0.7,
                        "description": "RBL: Sender listed at http://www.dnswl.org/, low"
                    },
                    {
                        "name": null,
                        "score": 0,
                        "description": null
                    },
                    {
                        "name": "listed",
                        "score": 0,
                        "description": "in list.dnswl.org]"
                    },
                    {
                        "name": "RDNS_NONE",
                        "score": 1.3,
                        "description": "Delivered to internal network by a host with no rDNS"
                    }
                ]
            },
            "dkim": {
                "signed": false,
                "valid": false
            },
            "email": "person@mail.mydomain.com",
            "tags": [],
            "sender": null,
            "template": null
        }
    }
]

问题是在架构 returns 的 text 键中,当我只需要回复时,整个电子邮件链。我已经尝试在我的电子邮件模板的顶部添加一个下划线,当我收到发布到我的服务器的 JSON 有效负载时,我只得到下划线以上的所有内容。但是,邮件客户端(我正在使用 Mac 的邮件客户端)添加日期、发件人、收件人等

我怎样才能解析它以便我可以只收到回复和任何签名而不是包含的不必要的文本?

下面是摆脱电子邮件链的有效代码片段:

$response = substr($request['msg']['text'], 0, strpos($input['msg']['text'], '_'));

上面的行从头开始获取字符串,直到它到达电子邮件模板中的 _。当有人回复时,电子邮件客户端会在我的模板上方添加(呈现下划线仅对删除响应附带的电子邮件链有用,而不是上面附加的内容),这就是我需要删除的部分,以便我只得到回复。

从视觉上看,这是客户所做的:

如您所见,它添加到 On Jan 22... 上,当 Mandrill API 将它发送到我的服务器时,我在解析它时试图消除它。

进一步搜索并找到了这个 repo:

https://github.com/willdurand/EmailReplyParser

此存储库在其 API getContent() 中有一个方法 returns 仅回复电子邮件。

这成功了。