Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using cfloop inside cfmail

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
I'm not sure what I've got wrong here, but if I take out the cfloop, the email will send properly. If the cfloop code stays, then the email doesn't get sent.

Can someone take a look for me?

Code:
<cfquery name="get_order_details" datasource="#ds#">
SELECT product_id, quantity, item_price
FROM order_details
WHERE order_id = '#page.order_id#'
</cfquery>

<CFMAIL TO="#page.cust_email#" FROM="#page.our_primary_email#" SUBJECT="Confirmation of Your Order." SERVER="#page.mailserv#">
================================================
     Order Confirmation
================================================

<cfif isDefined('page.first_name')>#page.first_name#</cfif>, thank you for ordering from us!
		
ORDER DETAILS:

Price	Quantity	Model Number		Product
-----	--------	------------		-------

<cfset cust_total=0>
<cfloop query="get_order_details">
<cfquery name="get_prod_details" datasource="#ds#">
	SELECT model_number, title
	FROM products
	WHERE product_id = #product_id#
</cfquery>
#item_price#	#quantity#	#get_prod_details.model_number#		#get_prod_details.title#
<cfset cust_total = cust_total + item_price>
</cfloop>
=========
#cust_total# 


Please shop with Us Again.

=================================================
             
=================================================
</CFMAIL>
 
Do you get any error messages? Erap your CFMAIL tags inside CFTRY/CFCATCH tags and either email the errors or display the errors on the screen.

Without looking at this closely I would assume the query call is dying the CFMAIL and thus not sending the mail.

Try something like this:
Code:
<cftry>

		<cfquery name="get_order_details" datasource="#ds#">
			SELECT product_id, quantity, item_price
			FROM order_details
			WHERE order_id = '#page.order_id#'
		</cfquery>
		
		<CFMAIL TO="#page.cust_email#" FROM="#page.our_primary_email#" SUBJECT="Confirmation of Your Order." SERVER="#page.mailserv#">
		================================================
			 Order Confirmation
		================================================
		
		<cfif isDefined('page.first_name')>#page.first_name#</cfif>, thank you for ordering from us!
				
		ORDER DETAILS:
		
		Price    Quantity    Model Number        Product
		-----    --------    ------------        -------
		
		<cfset cust_total=0>
		
		<cftry>
			<cfloop query="get_order_details">
				<cfquery name="get_prod_details" datasource="#ds#">
					SELECT model_number, title
					FROM products
					WHERE product_id = #product_id#
				</cfquery>
			
				#item_price#    #quantity#    #get_prod_details.model_number#        #get_prod_details.title#
				<cfset cust_total = cust_total + item_price>
			</cfloop>
		<cfcatch type="any">
			<cfmail to="" from="" subject="error">
				<cfdump var="#cfcatch#" label="cftry error caught - inner cfloop">
				<cfoutput>#cfcatch#</cfoutput>
				<cfabort>
			</cfmail>		
		</cfcatch>
		</cftry>
		=========
		#cust_total# 
		
		Please shop with Us Again.
		
		=================================================
					 
		=================================================
		</CFMAIL>

<cfcatch type="any">
	<cfmail to="" from="" subject="error">
		<cfdump var="#cfcatch#" label="cftry error caught - cfloop">
		<cfoutput>#cfcatch#</cfoutput>
	</cfmail>
</cfcatch>
</cftry>

This will help debug the issue.

[sub]
____________________________________
Just Imagine.
[sub]
 
Shouldnet these
#item_price# #quantity#

Be
#get_order_details.item_price# #get_order_details.quantity#
 
No because he is looping over a query, when you cfloop or cfoutput over a query you dont have to use query.fieldname
 
wbg34 is correct if your loop is inside a "cfoutput query". I Have seen it before. I wold be better to specify it anyway.
 
MochaLatte is actually correct. I am not using query.fieldname's and its working fine. I've done it probably a hundred times and never had a problem.

GUJUm0deL, thanks for your suggestions...I like that idea for debugging.

Actually, I figured out that the problem was my usual overlooking of details. I had put a query inside a loop that needed to be outside of the loop.

All is well now. Thanks everyone.
 
No because he is looping over a query, when you cfloop or cfoutput over a query you dont have to use query.fieldname

MochaLatte is actually correct. I am not using query.fieldname's and its working fine. I've done it probably a hundred times and never had a problem.

macromedia recomends you ALWAYS use the scope prefex when refering a variable unless you're using a variable in the "Variables" scope.
that means queryname.fieldname, form.fieldname, url.varname, session.varname, etc...

One of the cons of CF is, how easy it is to write "bad code" this is a perfect example. It's in everyones best interest if you follow the MM recomended best practices, for more efficient code and "self commenting code".

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top