0

I'm using SendGrid's email parser to return the body of an email to my application. The problem is, if the message is a reply it also returns the original message with the reply message. What I'm trying to do is parse out the reply from the original message. To do this I think I need to find the text/characters that each major email client uses between the reply and the original message and remove the original message.

What I'm trying to do is use ColdFusion to look at the following example text and delete out everything to the right of and including the On Fri, Sep 13, 2019 in the following text.This is an example of a gmail reply.

"here's a test reply with multiple line breaks 
On Fri, Sep 13, 2019 at 5:56 PM <matt@email.com> wrote: 
> ---*PLEASE REPLY ABOVE THIS LINE*--- 
> > This is your confirmation text 
> I wonder how to keep the line breaks? 
> > > Thank you! "

How would this be possible in CF? If you know of any better way to accomplish this, that would be great as well.

So the text I'd like to end up returning would be:

"here's a test reply with multiple line breaks"

--- Update #1 ---

Here's what I have so far. It seems to be working well to find if the keyword phrase is included in the body. How would I use the left() function with this to get the content before the match?

<cfset keyword = "this is ___ my keyword 33> phrase"> 
<cfset myString = "The dog sniffed at the this is ___ my keyword 33> phrase star fish On Sun, Sep 15, 2019 at and growled"> 
<cfset regEx = "\b"& keyword &"\b"> 
<cfif reFindNoCase(regEx, myString)> Found it <cfelse> No Match </cfif>
  • I found this post https://stackoverflow.com/questions/33960491/regex-match-whole-word-string-in-coldfusion which may help find the phrase. – user2666528 Sep 14 '19 at 01:52
  • What have you tried so far? – James A Mohler Sep 14 '19 at 07:06
  • 1
    Find the first pattern match using [reFindNoCase()](https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-m-r/refindnocase.html) (regular expression, returns the position of the match) and then substring the content using [left()](https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-l/left.html). – Alex Sep 14 '19 at 11:54
  • *parse out the reply from the original message* Fun, fun. Keep in mind there's no standard used by all email clients, so no regex will work 100%. See [this thread](https://stackoverflow.com/questions/278788/parse-email-content-from-quoted-reply) for common patterns of major email clients and discussions about some of the gotchas. – SOS Sep 14 '19 at 22:44
  • @Alex thank you, that seems like a good idea. I've got the reFindNoCase() working but how would I apply the left() function to this? I updated my original question with code example. – user2666528 Sep 15 '19 at 16:00

1 Answers1

0

I was able to do what I needed using the following:

<cfset keyword = "this is ___ my keyword 33> phrase"> 
<cfset myString = "The dog sniffed at the this is ___ my keyword 33> phrase star fish On Sun, Sep 15, 2019 at and growled"> 
<cfset regEx = "\b"& keyword &"\b"> 
<cfset findphrase = reFindNoCase(regEx, myString ,1,false,"one")>

<cfif findphrase>
    Found it 
<cfelse>
    No Match
</cfif>
<br />
<cfoutput>
Position:   #findphrase#<br /><br />
Reply: #Left(myString, findphrase-1)#
</cfoutput>