|
Mailing Lists
|
Home / Groups / Regular Expressions (RegEx)
Finding name and email in forwarded mailmessageI'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. |
February 11, 2012
|
Latest Fusion Authority Articles
|
|||||||||||||||||||||||||||||||||||||||||||||||||