Usually, when sending an email using ColdFusion's CFMail tag, I use very simple email addresses like this:
Launch code in new window » Download code as text file »
But, sometimes, when I want to up the level of professionalism, I will add the sender and recipient names to the email addresses:
Launch code in new window » Download code as text file »
Doing this will use the first value "Ben" as the name to display and will use the value in between the "<" and ">" as the user's email address. But what happens if that name contains a suffix like "Jr" or "Sr"? When I first came up against this, I tried to do what I had done before:
Launch code in new window » Download code as text file »
In this case, John Noble, Sr. is sending an email to his son, John Noble, Jr.. However, when I try to run the code above, I get the following ColdFusion error:
Attribute validation error for tag CFMAIL. The value of the attribute to, which is currently "John Noble, Jr. <john.jr@xxxxxxxxx.com>", is invalid.
The problem with this is that you can use ColdFusion's CFMail tag to send to multiple recipients by using a comma-delimited list of email addresses in the TO attribute:
Launch code in new window » Download code as text file »
Well, that is to say, that is not a problem. This is, in fact, a highyl beneficial feature of the ColdFusion language. But, my problem in this case is that the comma in the suffix name is confusing ColdFusion into thinking that there are multiple TO addresses. The trick to fixing this, as I just discovered, is to wrap the name of the sender or recipient in quotes so that ColdFusion knows to treat it as a whole value. This will, for lack of a better term, escape the comma in the CFMail attributes:
Launch code in new window » Download code as text file »
Notice that the names are wrapped in double-double quotes. Remember that in ColdFusion, in order to escape double quotes within a double-quote-quoted string value, you have to use two quotes in a row.
As it turns out though, ColdFusion provides an even easier way to handle this situation. If you don't want to use the quote escaping, you can swap up the order of the name and email address and place the display name within parenthesis:
Launch code in new window » Download code as text file »
This will accomplish the same exact thing with slightly more readable code. I am not sure if one offers advantages over the other outside of the need to escape the comma. So, that's how you escape commas within the user name of the ColdFusion CFMail tag.
Download Code Snippet ZIP File
Comments (4) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
ColdFusion Jobs And Developer Circuit's Distributed Flex Widget
ColdFusion UDF For Retrieving Surrounding Dates
I never knew Jurassic Park was a real place! =)
Posted by Sam on May 8, 2007 at 9:40 AM
Just don't be fooled. That was a professional dinosaur rider on a closed road. Do not try that at home.
Posted by Ben Nadel on May 8, 2007 at 9:46 AM
Thanks for the usefully Examples.
Posted by Online Druckerei on Dec 28, 2007 at 1:44 PM
Thanks Ben,
I experimented a while back and gave up. Nice to know the proper way to handle it. I wonder why this is not documented by Adobe... or, maybe it is and I missed it.
Anyway, thanks for the tip!
Posted by Vince Collins on Feb 18, 2008 at 9:59 AM