Using A Name Suffix In ColdFusion's CFMail Tag

Posted May 8, 2007 at 9:08 AM

Tags: ColdFusion

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 »

  • <cfmail
  • to="ben@xxxxxxxxx.com"
  • from="libby@xxxxxxxxx.com"
  • subject="Thanks for last night!">
  •  
  • Thanks for last night. I had a great time.
  • Let's do that again soon.
  •  
  • </cfmail>

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 »

  • <cfmail
  • to="Ben <ben@xxxxxxxxx.com>"
  • from="Libby <libby@xxxxxxxxx.com>"
  • subject="Thanks for last night!">
  •  
  • Thanks for last night. I had a great time.
  • Let's do that again soon.
  •  
  • </cfmail>

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 »

  • <cfmail
  • to="John Noble, Jr. <john.jr@xxxxxxxxx.com>"
  • from="John Noble, Sr. <john.sr@xxxxxxxxx.com>"
  • subject="Son, I found a girl for you"
  • type="HTML">
  •  
  • <p>
  • Dear Son,
  • </p>
  •  
  • <p>
  • This is a girl from your mother's drawing class. She
  • looks like a ton of fun. I really think you should
  • consider meeting her for drinks. Here's a URL:
  • http://flickr.com/photos/keppyslinger/477844455/
  • </p>
  •  
  • </cfmail>

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 »

  • <cfmail
  • to="dude@xxxx.com,chick@xxxxx.com,dog@xxxxxx.com"
  • from="ben@xxxxxx.com"
  • subject="To all of you!">
  •  
  • Hey guys, you all rock!
  •  
  • </cfmail>

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 »

  • <cfmail
  • to="""John Noble, Jr."" <john.jr@xxxxxxxxx.com>"
  • from="""John Noble, Sr."" <john.sr@xxxxxxxxx.com>"
  • subject="Son, I found a girl for you"
  • type="HTML">
  •  
  • <p>
  • Dear Son,
  • </p>
  •  
  • <p>
  • This is a girl from your mother's drawing class. She
  • looks like a ton of fun. I really think you should
  • consider meeting her for drinks. Here's a URL:
  • http://flickr.com/photos/keppyslinger/477844455/
  • </p>
  •  
  • </cfmail>

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 »

  • <cfmail
  • to="john.jr@xxxxxxxxx.com (John Noble, Jr.)"
  • from="john.sr@xxxxxxxxx.com (John Noble, Sr.)"
  • subject="Son, I found a girl for you"
  • type="HTML">
  •  
  • <p>
  • Dear Son,
  • </p>
  •  
  • <p>
  • This is a girl from your mother's drawing class. She
  • looks like a ton of fun. I really think you should
  • consider meeting her for drinks. Here's a URL:
  • http://flickr.com/photos/keppyslinger/477844455/
  • </p>
  •  
  • </cfmail>

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



I'm Too Young For This!

Reader Comments

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


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting