从Mandrill入站API返回时,解析电子邮件,仅回复

时间:2015-01-22 17:22:53

标签: php json email mandrill

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

这是Mandrill返回的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
        }
    }
]

问题是,当我只需要回复时,架构中的text键会返回整个电子邮件链。我尝试在电子邮件模板的顶部添加下划线,当我收到张贴到我的服务器的JSON有效负载时,我只获得了下划线之上的所有内容。但是,邮件客户端(我使用Mac的邮件客户端)添加了日期,来自,等等。

我如何解析这个问题,以便我能收到回复及其中的任何签名,而不是包含不必要的文字?

以下是摆脱以下电子邮件链的代码片段:

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

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

从视觉上讲,这就是客户所做的事情:

enter image description here

正如您所看到的那样,它添加在On Jan 22...上,这是我在Mandrill API将其发送到我的服务器时解析它时试图消除的。

1 个答案:

答案 0 :(得分:2)

搜索了一下,发现了这个回购:

https://github.com/willdurand/EmailReplyParser

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

这就是诀窍。