Ask a Jedi: Sending emails out over time.

This post is more than 2 years old.

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.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Jack posted on 5/20/2020 at 5:07 PM

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?

Comment 2 (In reply to #1) by Raymond Camden posted on 5/20/2020 at 7:02 PM

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.

Comment 3 (In reply to #2) by Jack posted on 5/20/2020 at 7:40 PM

Can this be done by coding in CF rather than on the server?

Comment 4 (In reply to #3) by Raymond Camden posted on 5/20/2020 at 7:50 PM

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.