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

Using Mandrill's API and their Inbound routing abilities, I'm trying to receive only the reply from the e-mail. I'm trying to parse just the latest response, similar to how Asana or Zendesk allow you to reply to an e-mail and it will end up in their database (rather than using their webapps).

This is the JSON payload that Mandrill returns:

[
    {
        "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
        }
    }
]

The problem is that in the text key in the schema returns the ENTIRE e-mail chain when I just need the reply. I've tried adding an underscore to the top of my e-mail template, and when I receive the JSON payload POSTed to my server, I get only everything above the underscore. However, mail clients (I'm using Mac's Mail client) add on dates, froms, tos, etc.

How am I able to parse this so I can receive JUST the reply and any signature with it and not the unnecessary text that's included?

Here's the snippet that's working that gets rid of the e-mail chain that comes below:

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

The line above gets the string from the beginning up until it hits the _ in the e-mail template. The e-mail client adds above my template when someone replies (rendering the underscore only useful for ridding the e-mail chain that comes with the response, and not what's tacked on above), and that's the bit I need to remove so that I get ONLY the reply.

Visually, this is what the client does:

As you can see it adds on On Jan 22... which is what I'm trying to eliminate when parsing it when the Mandrill API sends it to my server.

Answers


Searched around a bit more and found this repo:

https://github.com/willdurand/EmailReplyParser

This repository has a method in its API getContent() that returns only the e-mail reply.

This did the trick.


Need Your Help

Android - Starting a service with extra information through put/getExtra

android service android-intent extra

I've really tried to get through the intent.putExtra() and getIntent().getExtras() and apply them to one of the SimpleService tutorials. I know a lot of people have already asked "why is bundle ex...

CQWP pulling link list , when a link is clicked renders the list item rather than opening the URL itself

sharepoint hyperlink cqwp

I have a content query web part on page which i am using to pull a list of links (from a link list)to display on page. I thought clicking this link, user would be able to navigate to the link itsel...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.