A user wrote in with an interesting issue. He has to send emails out, but is limited by his host to no more than 250 emails per hour. How can he switch from "immediate" emails to a throttled limit?
While I'm not going to write the entire code out in this blog post, the problem isn't too terribly hard to solve if you use the ColdFusion Schedular.
What I would suggest is this approach: Instead of sending emails out, take each email and add the data to a database table. You would store the from, to, subject, and body. You also want to store the time the email was created.
Then set up a scheduled task. This take will run once per hour. To be safe, you may actually want to set the task to run every 65 minutes. This task will do a cfquery to grab the emails from the database table. It will grab only 250 rows, and grab the oldest ones first. Again to be safe with your host, you may want to grab 240 instead.
As you loop over the data, you will send each email, and then delete the record from the database.
In general this should work fine, but you obviously want to monitor the database table. If it grows faster than you can process, then you probably want to consider moving to a new host.
Archived Comments
Hi Raymond, this is a very old article not sure if what you said still apply today? There is no limit on my host but I want to avoid being marked as spammer by throttling the emails being sent to 250/hours spread out during the day. Is there a different way of doing this with newer CF today? Does it matter if we send 10K in one shot?
I honestly don't know if CF's added something like this to their mail support. It's been a while since I set up a CF server.
Can this be done by coding in CF rather than on the server?
Maybe I don't understand you - but I literally explained how to do code it in CF. Or do you mean avoiding the CF Admin? I believe recent CF servers let you define scheduled tasks from code. You could also use a third party service to setup a scheduled HTTP call to your CFM.