【问题标题】:Parsing e-mail, only reply, when returned from Mandrill Inbound API从 Mandrill Inbound API 返回时解析电子邮件,仅回复
【发布时间】:2015-03-21 14:35:10
【问题描述】:

使用 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 键会返回整个电子邮件链。我尝试在我的电子邮件模板顶部添加下划线,当我收到 POST 到我的服务器的 JSON 有效负载时,我只得到下划线上方的所有内容。但是,邮件客户端(我使用的是 Mac 的邮件客户端)添加日期、发件人、收件人等。

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

这是摆脱下面电子邮件链的有效 sn-p:

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

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

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

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

【问题讨论】:

    标签: php json email mandrill


    【解决方案1】:

    搜索了一下,发现了这个 repo:

    https://github.com/willdurand/EmailReplyParser

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

    这成功了。

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 2015-11-05
      • 2020-09-14
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多