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


cfmail
to="#ValueList(query.column)#"
from="#variables.from#"
subject="#variables.subject#"
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.
Simon