Server Load when Sending 1000 Emails at Once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zenabi
Forum Commoner
Posts: 84
Joined: Mon Sep 08, 2003 5:26 am
Location: UK

Server Load when Sending 1000 Emails at Once

Post by zenabi »

I am writing a script that will be automated to run once a day using a cron job. It will be similar to how job (recruitment) sites send emails to users every day based on their stored search criteria.

This is what I have worked out in my head so far:

At 1am every morning my script will (for every user in my database):
1. Run a SELECT COUNT query with the stored search criteria and a clause to search for jobs that were posted after the date that the previous email was sent. Eg. SELECT COUNT(*) FROM jobs WHERE location='London' AND post_date > $date_last_email_sent

2. If the count is greater than zero, then send an email to the user stating how many new jobs have been posted.

3. Update date_last_email_sent field with NOW().

Now if I have 1000 users in my database, this script will loop 1000 times and send 1000 emails (assuming all searches have matches).

What are the implications of this on a server? I'll only be using shared hosting. Would it be too heavy to run every day?

Is there a better way I can do this? I'd like to hear your thoughts.
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post by Trenchant »

Run it in sections. I would only send like 10 emails every 10 minutes.

So starting at 1:00 the server goes through and selects the first 10 people. It sends the emails and then updates a row saying those emails were sent. 10 minutes later it does the next 10 and so on.


You could probably get away with doing more than 10. Depends what type of server your webhost uses.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

search the forums for "mass mail" we've only talked about it a couple times. :?

Moved to PHP - Code.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Web Dummy wrote:So starting at 1:00 the server goes through and selects the first 10 people. It sends the emails and then updates a row saying those emails were sent. 10 minutes later it does the next 10 and so on.
I think Web's 10 minute delay is a good conservative number for your typical oversold shared server. However, I have host that have said every 10 seconds and some have said not on my server you won't. You should really talk to your host to find the definitive answer for your server. Be sure to check your terms of service too.
zenabi
Forum Commoner
Posts: 84
Joined: Mon Sep 08, 2003 5:26 am
Location: UK

Post by zenabi »

Thank you all for your help.

It seems the best way to do this is to use a delay method and not to use the mail() function (but use a method to close a socket connection after all the emails have been sent, as opposed to closing after each sent email).

I will investigate this further. Thanks again.
Post Reply