Search code examples
phpparsingemailhtml-email

Get the actual email message that the person just wrote, excluding any quoted text


There are two pre-existing questions on the site. One for Python, one for Java.

I want to be able to do pretty much exactly the same (in PHP). I've created a mail proxy, where two people can have a correspondance together by emailing a unique email address. The problem I am finding however, is that when a person receives the email and hits reply, I am struggling to accurately capture the text that he has written and discard the quoted text from previous correspondance.

I'm trying to find a solution that will work for both HTML emails and Plaintext email, because I am sending both.

I also have the ability if it helps to insert some <*****RESPOND ABOVE HERE*******> tag if neccessary in the emails meaning that I can discard everything below.

What would you recommend I do? Always add that tag to the HTML copy and the plaintext copy then grab everything above it?

I would still then be left with the scenario of knowing how each mail client creates the response. Because for example Gmail would do this:

On Wed, Nov 2, 2011 at 10:34 AM, Message Platform <35227817-7cfa-46af-a190-390fa8d64a23@dev.example.com> wrote:
## In replies all text above this line is added to your message conversation ##

Any suggestions or recommendations of best practices?

Or should I just grab the 50 most popular mail clients, and start creating custom Regex for each. Then for each of these clients, also a bizallion different locale settings since I'm guessing the locale of the user will also influence what is added.

Or should I just remove the preceding line always if it contains a date?.. etc


Solution

  • There are many libraries out there that can help you extract the reply/signature from a message:

    I've also read that Mailgun has a service to parse inbound email and POST its content to a URL of your choice. It will automatically strip quoted text from your emails: https://www.mailgun.com/blog/handle-incoming-emails-like-a-pro-mailgun-api-2-0/

    Hope this helps!