Sending CFMAIL To Multiple Recipients

Posted By : todd sharp Posted At : December 12, 2006 1:49 PM Posted In: ColdFusion

10

This is totally basic stuff, but I think sometimes people overlook the small easy stuff. A friend just asked about an easy way to send an email to multiple recipients where the email address was a query in the column that he needed to loop over and send a message to each user.

My first reaction was to hit the ColdFusion Cookbook and find a quick example for him, but surprisingly there wasn't an example of this over there yet. On an related note - I'm not paid by Ray Camden to advertise all of his sites - though it may seem that way. They're just that damn good that I can't help but to mention them frequently.

Since I didn't find anything there I whipped up the following quick example to show him how to do this utilizing the query attribute of cfmail.

<!--- create a fake query --->
<cfset q = queryNew("emailAddress")>
<cfset queryAddRow(q, 2)>
<cfset querySetCell(q, "emailAddress", "me@you.com", 1)>
<cfset querySetCell(q, "emailAddress", "you@us.com", 2)>

<!--- set up variables --->
<cfset variables.msg = "this is a test">
<cfset variables.subject = "test subject">
<cfset variables.from = "admin@coldfusionites.com">

<cfmail from="#variables.from#" subject="#variables.subject#" query="q" to="#emailAddress#">
#variables.msg#
</cfmail>

I may submit to the cookbook - unless this seems too trivial and simple. Thoughts?

Comments (10)

James Edmunds's Gravatar Definitely submit it, because if one person was looking for it (that's YOU!), then someone else may be looking for it. The whole point of this kind of resource is to have an example of code to grab, modify, etc.!

Rich Rein's Gravatar We have also used #ValueList(query.column)# in similar situations...

Rich Rein's Gravatar I should have added to the above, we would be doing something like this:

cfmail
to="#ValueList(query.column)#"
from="#variables.from#"
subject="#variables.subject#"

todd sharp's Gravatar Valid point Rich - but doing so would expose email addresses to all recipients. My method ensures the only person in the To line is the individual recipient.

Chuck J's Gravatar Definitely submit it - it seems most of the ColdFusion blog owners/tutorial authors/etc. are experts, but many of the people searching for examples are less than experts. I may even put a <cfquery> block in there with a dummy query rather than create a fake query. Also, many people in hosted cf enviros need to specify the "server" attribute in cfmail.

Derek P.'s Gravatar I tend to do a cfmail inside of a cfloop...because often times, I am not just sending an email, I want something else to happen with the data I am emailing out or what not. just my 2 cents :P

dan's Gravatar Todd,
Will this method send one email to many recipients or several emails? If this is only sending 1 email it could be a big problem. From a mail standpoint cf aside, if I fire up my mail client and send it to 10 people (10 addresses in the to column) it will only go through if all email addresses are valid. I agree with chuck that a loop works out better because for each client i have a "client" log and a "client mail" log, looping allows me to write to each of those logs when i need to.

todd sharp's Gravatar It will send a seperate email to each recipient. It's basically a shortcut to putting cfmail in a loop.

Ben Nadel's Gravatar Also, one thing that I have seen is people will email the message to themselves and then do a ValueList() (like mentioned above) and put that in the BCC field. That way, all the emails on the BCC list will get the message but not know about other recipients.

Newtriks's Gravatar Awesome Todd, just the trick thanks very much :)

Simon