House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home / Groups / Regular Expressions (RegEx)

Finding name and email in forwarded mailmessage

Author:
Peter Boughton
02/06/2009 05:05 PM

I'm not 100% certain what you're asking, but I have attempted to provide what I think you want... Assuming EmailData contains a full email source, you can try using the following code. (I've commented all my comments, so you can simply copy & paste if you like) <!---   Emails are required to use two-char linebreaks, which is stupid, but at least we can remove them easily enough... ---> <cfset EmailData = replace( EmailData , Chr(13)&Chr(10) , Chr(10) , 'all' )/> <!---   Headers are seperated from content with a blank line, so we use \n\n to find the end of the headers, then remove that from the source to get the content. ---> <cfset EmailHeaderData = rematch( '(?s)\A.*?(?=\n\n)' , EmailData  )[1]/> <cfset EmailContent = replace( EmailData , EmailHeaderData , '' ) /> <!---   And each header is delimited from it's content with a colon and space, but can be multiline.   So start by getting each header string into an array, then convert into a key-value structure. ---> <cfset EmailHeaderData = rematch( '(?si)(^[a-z-]+: ).*?(?=^[a-z-]+: )' , EmailHeaderData )/> <cfloop index="i" from="1" to="#ArrayLen(EmailHeaderData)#">   <cfset EmailHeaders[ ListFirst(EmailHeaderData[i],':') ] = trim(ListRest(EmailHeaderData[i],':')) /> </cfloop> <!---   Finally, Name and Email are usually in Name <Email> format.   Sometimes Name is quoted. ---> <cfset Name = rereplace( EmailHeaders.From , '^"?(.*)"? <[^>]+>' , '\1' ) /> <cfset Email = ListLast( EmailHeaders.From , '<>' )/> (I haven't confirmed, but I think if an email doesn't have a name it is sent as "Email" <Email>) And that's pretty much it, I think. The above code requires CF8 or Railo 3 for the rematch function. Obviously, if you'd like any specific parts/regexes explained, let me know.


Search regex

February 11, 2012

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29