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

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

May 8, 2007 at 9:40 AM // reply »
76 Comments

I never knew Jurassic Park was a real place! =)


May 8, 2007 at 9:46 AM // reply »
7,538 Comments

Just don't be fooled. That was a professional dinosaur rider on a closed road. Do not try that at home.


Dec 28, 2007 at 1:44 PM // reply »
2 Comments

Thanks for the usefully Examples.


Feb 18, 2008 at 9:59 AM // reply »
2 Comments

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!


Sep 9, 2009 at 5:58 PM // reply »
1 Comments

I tried all this and none of it works. Still get the error.


Sep 12, 2009 at 10:23 PM // reply »
7,538 Comments

@Dusty,

What error is it giving you?


Sep 17, 2009 at 8:45 AM // reply »
2 Comments

Thank you thank you thank you! This is EXACTLY what I've been looking for!


Sep 17, 2009 at 10:48 AM // reply »
2 Comments

It's clear that she was riding the dinosaur under controlled conditions, thanks for the warning Ben :)

@Dusty, I got it up and running now. Post the error please?


Oct 20, 2009 at 8:20 PM // reply »
1 Comments

The only one of these that worked, using CFMAIL and SmarterMail, was the double-double quote approach. Thanks very much for this helpful page. Bob mack


Oct 31, 2009 at 4:17 PM // reply »
7,538 Comments

@Robert,

Yeah, that's the one I've found to be the most universal.


Nov 2, 2009 at 4:46 PM // reply »
3 Comments

I noticed this only became an issue after I upgraded from MX7 to CF9. I had a cfmail tag that in the from address had "Companyname, Inc <test@email.com> and this worked perfectly in MX7.

After upgraded to 9, all my mail started going in the undeliverable folder. Took a while to figure it out but thanks for pointing this out Ben.


Nov 3, 2009 at 12:29 PM // reply »
7,538 Comments

@Rob,

That's good to know - I haven't upgraded to CF9 yet, but I'll keep my eyes open.


Jan 20, 2010 at 2:40 PM // reply »
3 Comments

I just ran into the comma problem. Thanks for this post, it solved my problem.


Jan 20, 2010 at 10:51 PM // reply »
7,538 Comments

@Don,

Glad to be here to help.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 17, 2010 at 11:36 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
@Ben, It is. But I have tested again and sleep() seem to be keeping windows hanging. As a result, multiple people were having issues uploading and removing images when they are trying to do the imag ... read »
Mar 17, 2010 at 11:22 PM
Google Maps Not Working in Internet Explorer (IE)
@James The page has a problem in IE7, because line 112 has a comma at the end. Line 112 is the first problem, there are many more with the same. Ralph ... read »
Mar 17, 2010 at 8:39 PM
Looping Over ColdFusion JSON Queries In jQuery
Thanks Ben - this is just what I needed - just getting up to speed with jQuery - and your posts are accelerating that process substantially :) ... read »
Mar 17, 2010 at 7:50 PM
ColdFusion ArraySplice() Method As An Example Of A Dynamic Method Signature
Java is awesome and I love seeing new twists on it. @Ben - I totally agree with you. ... read »
Mar 17, 2010 at 4:13 PM
Testing For NULL Values In A ColdFusion Query Result Set
To get around the empty strings in an UPDATE statement I pretended the variable was a string by enclosing it in single quotes, CAST it to an integer, let it convert empty strings to zeroes, and used ... read »
Mar 17, 2010 at 4:12 PM
Ask Ben: Environment-Based Application.cfc Settings
Ben, those are valid comments. I posted some code that shows how to "worryfree ;o)" copy code and let the Application.cfc sort it out. http://boncode.blogspot.com/2010/03/cf-dynamically-changing-ap ... read »
Mar 17, 2010 at 3:32 PM
Using Appropriate Status Codes With Each API Response
@Marc, For our project it isn't really a requirement, since most (if not all) of our resources are private and related to the logged in user. Anyway, IIRC google does execute javascript before inde ... read »
Mar 17, 2010 at 3:24 PM
Finally Finished Ayn Rand's Atlas Shrugged Audio Book
Objectivism is a form of positivism, and Quantum Mechanics does not fit positivism. One of the postulates of Quantum Mechanics is the impossibility of separating the object of measurement from the me ... read »