Sherwin Flight
Sherwin Flight

Reputation: 2363

E-Mail in PHP, message threading, regex probably needed

I'm working on something that logs e-mail messages sent to a certain address. I have a PHP script that puts the info into MySql, which is working fine.

I need to be able to group messages based on the 'conversation' similar to Gmail, and have already read up on this a bit. This doesn't need to be perfect because messages will be manually approved before being shown on the website, and any errors can be corrected then. I just want to make it the least amount of work as possible so that linking a new e-mail to the original should be done automatically.

My understanding is that the In-Reply-To header can identify the original message, but that its use is not standard.

I found this on another page:

The most common forms of In-Reply-To seemed to be:

31%     NAME's message of TIME <ID@HOST>
22%     <ID@HOST>
9%      <ID@HOST> from NAME at "TIME"
8%      USER's message of TIME <ID@HOST>
7%      USER's message of TIME
6%      Your message of "TIME"
17%     hundreds of other variants (average 0.4% each?)

However, this seems to indicate that it's not unreasonable to assume that, if there is an In-Reply-To field, then the first <> bracketed text found therein is the Message-ID of the parent message.

So, what is the easiest way to get that value? Is there a regular expression that would let me grab whatever is inside the < and > if is is available? (According to the post I found, should this be the FIRST value inside a < and > ?)

Thanks for any help you can provide.

Upvotes: 1

Views: 346

Answers (1)

Matt
Matt

Reputation: 14531

You should be able to just grab the first match in the back-reference:

<(.+)>

Explanation:

//    Match the character “<” literally «<»
//    Match the regular expression below and capture its match into backreference number 1 «(.+)»
//       Match any single character that is not a line break character «.+»
//          Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
//    Match the character “>” literally «>»

According to this documentation you could write something like as follows to accomplish what you want to do (forgive me if this is not valid php):

$string ='blah blah blah <ID@HOST>';
preg_match('/<(.+)>/', $string, $match);
print_r($match);

Upvotes: 2

Related Questions