tarnfeld
tarnfeld

Reputation: 26556

Sleep between calls of Mail() in PHP

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....

Upvotes: 1

Views: 5785

Answers (5)

tarnfeld
tarnfeld

Reputation: 26556

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!

Upvotes: 0

Mr. Smith
Mr. Smith

Reputation: 5558

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.

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 400922

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 :

Upvotes: 3

astropanic
astropanic

Reputation: 10939

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.

Upvotes: 2

Related Questions