cfmail sending multiple emails when looping over a query

734 views
Skip to first unread message

kencl

unread,
Nov 28, 2011, 4:42:03 PM11/28/11
to Railo
Hi Folks,

I'm having an odd problem sending e-mail using cfmail with a query
param. It is supposed to send a single e-mail which contains the
resultset from a database query (the contents of a shopping cart).
What's happening, however, is that it is sending 1 email per record in
the resultset, where the first e-mail contains the first record, the
second contains both the first and second records, the third contains
the first 3 records etc. Has anyone else seen this behavior?

It's a low volume online store so the mail server's not getting
thrashed. Where would you begin troubleshooting, or would you just
stuff the cart into a single var and output it in one shot? While
this is a tempting, quick fix, exprience tells me it's better to
address the issue because you never know what else will go hinky when
you ignore oddities like this.

This code, simplified for brevity, has worked without issue for many
years:

<cfmail query="qName" to=...>
Thanks for your purchase #valued_customer_name#
<cfoutput>
Item: #product_name#
qty: #how_many#
price: #DollarFormat(unit_cost)#
total: #DollarFormat(VAL(how_many) * VAL(unit_cost))#
</cfoutput>
Shipping and Handling: #DollarFormat(shipping_charge)#
etc
</cfmail>

Your thoughts are appreciated.

>> Ken Clarke
>> Contract Web Programmer / E-commerce Technologist

Todd Rafferty

unread,
Nov 28, 2011, 5:39:15 PM11/28/11
to ra...@googlegroups.com
You need to show us your loop code. Your brevity is not helping us debug.
--
~Todd Rafferty
Volunteer
Community Manager
Railo Server - Open Source
----

AJ Mercer

unread,
Nov 28, 2011, 9:55:17 PM11/28/11
to ra...@googlegroups.com

kencl

unread,
Nov 28, 2011, 10:57:12 PM11/28/11
to Railo
The <cfoutput> is where the loop is. The vars within that loop are
all records from the query named in the cfmail tag.

The query would be something like
SELECT product_name, how_many, unit_cost
FROM shoppingcart
WHERE cartid = "#session.usercartid#"

Here is an actual example from a few hours ago. The first e-mail
contained only 1 record. It should not have been sent:
=============
Your Order:

Item: twin or full size softside bed
Qty: 1
Size: Full
Unit Price: $99.95
Product Total: $99.95

Shipping and Handling: $0.00
SubTotal: $879.95

Total: $879.95
==========

The second e-mail, which contained 2 records, was the correct e-
mail. This was the only message which should have been sent.
======================
Your Order:

Item: Plush top Softside Waterbed with Free Flow Tubes
Qty: 1
Size: Full set (mattress and foundation)
Unit Price: $780.00
Product Total: $780.00

Item: twin or full size softside bed
Qty: 1
Size: Full
Unit Price: $99.95
Product Total: $99.95

Shipping and Handling: $0.00
SubTotal: $879.95

Total: $879.95
==============

Notice that the values outside the loop are correct (they are
calculated before entering cfmail), but they only appear correct in
the second e-mail. It appears that the cfoutput is erroneously
triggering the sending of the message after each iteration.

Brad Wood

unread,
Nov 28, 2011, 5:41:51 PM11/28/11
to ra...@googlegroups.com
I don't know if I'm following everything going on there, but when you specify the "query" attribute of the cfmail tag, isn't the expected behavior that one E-mail would get sent per record in the result set?

~Brad

Sent from my ASUS Eee Pad

Azadi Saryev

unread,
Nov 28, 2011, 11:53:53 PM11/28/11
to ra...@googlegroups.com
if you query does not contain email recipients - do not use it in query attribute in cfmail tag:
<cfmail to="..." from="..." subject="...">
...
<cfoutput query="qName">
...
</cfoutput>
</cfmail>

kencl

unread,
Nov 29, 2011, 9:19:09 PM11/29/11
to Railo

On Nov 29, 12:53 am, Azadi Saryev <azadi.sar...@gmail.com> wrote:
> if you query does not contain email recipients - do not use it in query
> attribute in cfmail tag:

This method of outputting a recordset into a single e-mail is right
out of the developer manual. See http://perlprogrammer.net/query_based_email.pdf
It's page 1196 of "Developing Adobe ColdFusion 9 Applications". The
"Reviewing the code" section explains exactly why this works.

This worked for a decade (well, the original developers wrote the site
in CF5, whatever era that was :).

Todd Rafferty

unread,
Nov 29, 2011, 9:26:59 PM11/29/11
to ra...@googlegroups.com
He's right ( It's there in the help doc too.  http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f8e.html  ). And, I'll be damned. I learned something new.

I suggest opening a ticket at this point.

Todd Rafferty

unread,
Nov 29, 2011, 9:39:34 PM11/29/11
to ra...@googlegroups.com
Also, looking over the original code again, I highly suggest scoping your vars.

<cfmail query="qName" to=...>
Thanks for your purchase #valued_customer_name#
<cfoutput>
   Item: #qName.product_name#
   qty: #qName.how_many#
   price: #DollarFormat(qName.unit_cost)#
   total: #DollarFormat(VAL(qName.how_many) * VAL(qName.unit_cost))#
</cfoutput>
Shipping and Handling: #DollarFormat(shipping_charge)#
etc
</cfmail> 

Michael Offner

unread,
Nov 30, 2011, 3:02:58 AM11/30/11
to ra...@googlegroups.com
please open a ticket with much details possible

tnx micha

2011/11/30 Todd Rafferty <to...@getrailo.org>

kencl

unread,
Nov 30, 2011, 10:50:24 AM11/30/11
to Railo
ticket opened: https://issues.jboss.org/browse/RAILO-1649

The work around suggested by Azadi Saryev to move the query parameter
from the cfmail tag to the cfoutput tag seems to solve the problem.

Reply all
Reply to author
Forward
0 new messages