For too long now, my previous posts on the topic of passing emails to ColdFusion have promised that a future post will deal with how to parse emails in ColdFusion. So I can now happily (and somewhat sheepishly) announce that code for parsing is finally here.
I have a new project up called emailParse.cfc. You pass it the source text of an email, and it returns a struct with all headers, body parts, and attachments parsed out. There are even functions to forward the email on to another address, to try to get the email's context (ie, whether it's a hard bounce, soft bounce, out-of-office reply, or otherwise), and whether the email might be part of a loop. The whole idea is to let you programmatically deal with the emails that come into your server in real time, instead of relying on intermittent CFPOP calls from a scheduled task.
So feel free to try out the email parsing code, and let me know what you think.

Comments (4)
August 25, 2007
5:12AM | #
I was going to write something like this to detect bounced emails and flag the bad email addresses up in our database. I agree that CFPOP isn't up to scratch to handle large amounts of emails and it can't even read bounced emails because they tend to use UTF-7.
Can I ask, Tom, does it return the original "To" email address if the email has bounced? I found that some mail servers put the original address in different places so there isn't a one size fits all way of finding it.
August 25, 2007
8:41AM | #
@Gary: I'm glad you think this CFC is useful. Regarding the "To" property, you're right that it can be difficult to identify the original, intended recipient. The "To" property of my CFC will always contain the address of whoever sent the urrent message-- which for bounces is most likely a postmaster or similar address, instead of the address of the original recipient. However, in bounce messages from a server that supports VERP there's often a header called "Envelope-sender" or "Final-recipient" containing the original To address. If neither of those headers are present, then you could perhaps search the message body for "Final-Recipient:" and check to see if the original address comes after it.
August 25, 2007
9:50AM | #
I hope to impliment a solution in the next couple of months... perhaps I will mod your cfc to include an "original_To" and "delete_email" function if that's okay.
I'm not sure if CFPOP in CF8 has improved but I may never find out as I've been waiting for Adobe to send me a paid-for upgrade for the last month, despite many phone calls and emails.
April 29, 2008
5:13PM | #
Hi Tom,
Just wanted to say thanks for the cfc. I've been looking for something that would reliably kick back info on what type of bounce the email was and the original email. The functions in this cfc made it dead-easy.
However, I am afraid I totally used this in a way you hadn't intended it, ie. on a cfpop + cfloop. For what I as looking for (basically a way to process bounced emails after a monthly newsletter send out) a time delay was no issue and this was the quickest/easiest solution. I modified it slightly to remove some unused functions and arguments, leaving basically only the body + subject. Currently running on batches of 250 emails at a time, whittling down a 5000 email noreply account. 250 emails takes about 10 minutes.
I can provide code back to you if you want to put it out for other users who don't have access to raw emails and want a simple bounce management solution. Hit me up on above email address, though heading off for holidays tomorrow, so might not get back to you before next week if you want it.
Thanks again!! Saved me a lot of trouble!