Is There An Even Better Way?
That depends on your needs, but this method looks very promising. You must either have access to the ColdFusion Administrator or the right to use the CFSchedule tag on your server.
Dov Katz suggested this idea to us on the CF-Talk mailing list when describing his own scheduled mailer system. It works like this:
You dump whatever it is you are going to mail out in the myMessages database table exactly as we described in our revised mail trickler page. However, rather than calling the mail trickler after building your mailing list, you do nothing (sort of).
It would be more accurate to say you don't need to do anything more because you already set up the mail trickler in advance. Here's the deal: CFSchedule the code at the bottom of this page to run every two minutes.
Thats it. If nothing is there to send, the template will be a quick blip on your server's radar. If there is something there, then it will send 150 emails out every 2 minutes, which is a throughput rate of 4500 per hour. You say you can get away with a faster send rate? Fine. Set variables.QueryRun to a higher number. Don't need that much throughput? Its a good idea to widen the time between mailer intervals, as doing it once every two minutes means this routine runs 720 times per day.
This routine has the advantage of being a completely server-side solution based entirely within ColdFusion. It wipes out the need for the user to keep the browser window open while the mail trickles out. And it does all this without using a sleep scheme that ties down one of your limited, precious server processes.
HOWEVER it is not as flexible in some ways as the 'old' method, which could be used to send, for example, 15 messages every 5 seconds (10,800/hour); a nice slow-and-steady rate that delicately spoon-feeds only a few messages at a time to your mail server, at a rate tied to ColdFusion's fastest mail spool fetch rate, and which winds up being a much higher capacity. Can you run this routine at closer intervals, or at higher throughput rates? Is running a scheduled task that often just plain crazy?
Good question. Tell us your experience.
<cfset variables.QueryRun=150> <!--- pull the ID field so we can get a record count. ---> <cfquery datasource="#request.myDSN" name="MailList"> SELECT myMessages.ID FROM myMessages WHERE myMessages.EmailAddr IS NOT NULL ORDER BY myMessages.ID ASC </cfquery> <!--- Did we find anything to send? ---> <cfif MailList.RecordCount gt 0> <!--- Send as much mail as the QueryRun limit allows. ---> <cfloop query="MailList" startrow="1" endrow="#variables.QueryRun#"> <cfquery name="ThisEmail" datasource="#request.myDSN#"> SELECT myMessages.EmailAddr, myMessages.EmailMsg, myMessages.EmailType, myMessages.EmailServer, myMessages.EmailSubject, myMessages.EmailFrom FROM myMessages WHERE myMessages.ID= <cfqueryparam cfsqltype="CF_SQL_NUMERIC" value="#MailList.ID#"> </cfquery> <cfmail to="#ThisEmail.EmailAddr#" from="#ThisEmail.EmailFrom#" subject="#ThisEmail.EmailSubject#" server="#ThisEmail.EmailServer#" type="HTML"> <cfmailparam name="Message-ID" value="<#CreateUUID()#@#ThisEmail.EmailServer#>">
#ThisEmail.EmailMsg# </cfmail> <cfquery datasource="#request.myDSN#"> DELETE FROM myMessages WHERE myMessages.ID= <cfqueryparam cfsqltype="CF_SQL_NUMERIC" value="#MailList.ID#"> </cfquery> </cfloop> </cfif>
|