Sending emails with 2 minute interval
Hi all,
I am really scratching my head on this. I am trying to write a method that sends out 500 emails at a time with 2 minute interval between each group of 500 emails. My first attempt is to write a recursive function that calls itself after 2 minute intervals. However, at the beginning of the second iteration, where I do cfquery to get list of emails, I would get "The request has exceeded the allowable time limit Tag:CFQuery".
I am not sure why the time-out because i am doing a fresh cfquery. If I lower the interval to 10 seconds, the function works okay. But I need it to be 2 minutes to conform to the host company's policy. I have also tried doing it in a simple cfloop, but faced with the same error.
How do I fix this?
Assume 1000 email addresses, so 2 iteration only. Using MS SQL server.
<cffunction name="sendEmails">
<cfargument name="loopIdx">
<cfargument name="totalLoopNum" default="2">
<!--- get email addresses --->
<cfquery name="getEmails_qr">
select top 500 emails
from emailList
<cfif arguments.loopIdx gt 1>
where emails not in (select top #(idx-1)*500# emails from emailList order by emailNum)
</cfif>
order by emailNum
</cfquery>
<!--- send out emails now --->
<cfoutput query="getEmails_qr"><cfmail> ... </cfmail></cfoutput>
<cfif arguments.loopIdx lt arguments.totalLoopNum>
<!--- sleep for 2 minutes then calls itself --->
<cfscript>
interval = 120;
thread = createObject("java", "java.lang.Thread");
thread.sleep(javaCast("long", 1000*interval));
</cfscript>
<cfset sendEmails(arguments.loopIdx+1, arguments.totalLoopNum)>
</cfif>
</cffunction>
