Skip to main content
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Ajay Sathuluri
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Ajay Sathuluri ( @sathuluri )

Using A Name Suffix In ColdFusion's CFMail Tag

By on
Tags:

Usually, when sending an email using ColdFusion's CFMail tag, I use very simple email addresses like this:

<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:

<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:

<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://sample-images.com/foo/bar/photo.jpg
	</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:

<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:

<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://sample-images.com/foo/bar/photo.jpg
	</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:

<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://sample-images.com/foo/bar/photo.jpg
	</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.

Want to use code from this post? Check out the license.

Reader Comments

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!

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?

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

6 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.

15,643 Comments

@Omar,

I find CSS in email to be hit or miss. I tend to use inline CSS to get the most stable results. But, I am far from the best guy to talk to about this; I don't send out too many emails.

15,643 Comments

@Omar,

Interesting. I am not sure why that would work. It must escape the Style tags (allowing the Link tag to execute).

1 Comments

I always know that when I google any CF problem and end up on this guy's website, I've found the solution. Thanks Ben for all your support. This, as usual, was exactly what I was looking for.

1 Comments

Hi Ben
I was wondering if you knew how to display special chars in the cfmail subjuct line.

I want & not to be &amp! ?

5 Comments

Adobe should hire Ben as the official solution site for ColdFusion issues/questions, or at least throw some love his way.

Thanks for saving me a bundle of time!

3 Comments

Hello, I am new to coldfusion, I have a problem with sending mail within the code I want to put the values ??of a specific mail server, SMTP, USER, PASSWORD, PORT. ETC. Thank you.

3 Comments

Hello, I am new to coldfusion, I have a problem with sending mail within the code I want to put the values ??of a specific mail server, SMTP, USER, PASSWORD, PORT. ETC. Thank you.

3 Comments

Hello, I am new to coldfusion, I have a problem with sending mail within the code I want to put the values ??of a specific mail server, SMTP, USER, PASSWORD, PORT. ETC. Thank you.

61 Comments

Hi Ben, how come all unknown commenters (no known Gravatar) are blessed with an image of Arnie? An inside joke? Or an example for all?

2 Comments

Ben,

I tried this and see \"\"\"Mike Letson\ in the "To" field. Not quite sure where all those slashes are coming from.

Thoughts?

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel