Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, May 7, 2016

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

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

Answer by sascha for Get the actual email message that the person just wrote, excluding any quoted text


Afaik, (standard) emails should quote the whole text by adding a ">" in front of every line. Which you could strip by using strstr(). Otherwise, did you trie to port that Java example to php? It's nothing else than Regex.

Even pages like Github and Facebook do have this problem.

Answer by Hontvri Levente for Get the actual email message that the person just wrote, excluding any quoted text


Just an idea: You have the text which was originally sent, so you can look for it and remove it and additional surrounding noise from the reply. It is not trivial, because additional line breaks, HTML elements, ">" characters are added by the mail client application.

The regex is definitely better if it works, because it is simple and it perfectly cuts the original text, but if you find that it frequently does not work then this can be a fallback method.

Answer by geerlingguy for Get the actual email message that the person just wrote, excluding any quoted text


Unfortunately, you're in for a world of hurt if you want to try to clean up emails meticulously (removing everything that's not part of the actual reply email itself). The ideal way would be to, as you suggest, write up regex for each popular email client/service, but that's a pretty ridiculous amount of work, and I recommend being lazy and dumb about it.

Interestingly enough, even Facebook engineers have trouble with this problem, and Google has a patent on a method for "Detecting quoted text".

There are three solutions you might find acceptable:

Leave It Alone

The first solution is to just leave everything in the message. Most email clients do this, and nobody seems to complain. Of course, online message systems (like Facebook's 'Messages') look pretty odd if they have inception-style replies. One sneaky way to make this work okay is to render the message with any quoted lines collapsed, and include a little link to 'expand quoted text'.

Separate the Reply from the Older Message

The second solution, as you mention, is to put a delineating message at the top of your messages, like --------- please reply above this line ----------, and then strip that line and anything below when processing the replies. Many systems do this, and it's not the worst thing in the world... but it does make your email look more 'automated' and less personal (in my opinion).

Strip Out Quoted Text

The last solution is to simply strip out any new line beginning with a >, which is, presumably, a quoted line from the reply email. Most email clients use this method of indicating quoted text. Here's some regex (in PHP) that would do just that:

$clean_text = preg_replace('/(^\w.+:\n)?(^>.*(\n|$))+/mi', '', $message_body);  

There are some problems using this simpler method:

  • Many email clients also allow people to quote earlier emails, and preface those quote lines with > as well, so you'll be stripping out quotes.
  • Usually, there's a line above the quoted email with something like On [date], [person] said. This line is hard to remove, because it's not formatted the same among different email clients, and it may be one or two lines above the quoted text you removed. I've implemented this detection method, with moderate success, in my PHP Imap library.

Of course, testing is key, and the tradeoffs might be worth it for your particular system. YMMV.

Answer by philfreo for Get the actual email message that the person just wrote, excluding any quoted text


Possibly helpful: quotequail is a Python library that helps identify quoted text in emails

Answer by Andris for Get the actual email message that the person just wrote, excluding any quoted text


https://mailgun.com offers reply extaction (removing quoted content) and also signature extraction as cloud based ser. I'm yet to test it, but looks promising.

Answer by radzserg for Get the actual email message that the person just wrote, excluding any quoted text


I agree that quoted text or reply is just a TEXT. So there's no accurate way to fetch it. Anyway you can use regexp replace like this.

$filteringMessage = preg_replace('/.*\n\n((^>+\s{1}.*$)+\n?)+/mi', '', $message);  

Test https://regex101.com/r/xO8nI1/2


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.