views:

529

answers:

5

How would i send an email, to say 3000 recipients - with a Max 500 emails / hours on my dedicated IP? So far my thought is to send each email every 9 seconds, this would come to about 450 emails an hour... but how could i do this?

My plan for the sending of the emails would be the following...

$emails = ARRAY OF EMAILS, MYSQL RESULT
for($emails){
mail($subject,$row[email],$headers);
}

This wont work, wrong kind of statement but this concept anyway....

+2  A: 

Store You messages for sending in a database, mark messages which are sent. In a cron job select some of them that are not sent, and process them. The frequency of the cron job determines the speed of sending the emails.

astropanic
to user asking: moreover, you can not let PHP script run indefinitely - most servers will limit script run time.
dusoft
+1  A: 

What I would do is :

  • create a PHP script that is launched via cron once per hour
  • this script only sends 450 e-mails, at its own speed
  • when the 450 mails are sent, the script dies
  • and some time later, it is re-launched, by cron, to send 450 other mails.

The trick is : you have to know which mails where already sent.
Ordering the mails by id in your DB, or something like that, and using limit, would be OK, I suppose

If you want to sleep for a while between mails, use the sleep function ; something between 2 and 5 seconds would probably be OK, to be sure you script the chunk of 450 mails is finished before the script is re-launched by cron.


And, thinking about it :

  • You should put some logging stuff in place : if someone complains, saying he received 10 emails, it could help you find out why.
  • I wouldn't use the mail function : there are plenty of other possibilities, using libraries that are well-tested and provide lots of functionnalities, already developped : don't re-invent the wheel ;-)

Here are a couple of libraries I can think about :

Pascal MARTIN
SwiftMailer ships with a plugin called "Throttler", take a look at its documentation at http://swiftmailer.org/docs/throttler-plugin-howto
VolkerK
A: 

You could use this very handy Timer class to do the heavy lifting for you (start, stop and get the elapsed time within your loop, etc): PHPClasses: Timer.php.

Mr. Smith
+1  A: 

SwiftMailer does it this for you:

Alix Axel
Which basically uses the sleep command
AntonioCS
A: 

Hi Thanks for all the answers! The best way i found was actually to simply sleep() between calls using the sleep() as i tested 400 mails, this took 17 seconds :)

It is unlikely the user will send more than the 450 limit ... but if they do i have an if statement before the while() ends checking if there are more than 450 rows, if so it will sleep between each... this works without fiddly databases :)

Thanks!

tarnfeld