1

I'm developing a script which pulls thousands of emails out of my database for PHPMailer to send an email to.

I'm currently adding each of the emails in a loop which uses $mail->addAddress()

Is it possible to create a 5 second delay in between each email sending just to ensure there isn't a mail server overload?

Oliver Kucharzewski
  • 2,523
  • 4
  • 27
  • 51
  • possible duplicate of [How to send 100,000 emails weekly?](http://stackoverflow.com/questions/3905734/how-to-send-100-000-emails-weekly) – Chris Baker Jul 12 '14 at 06:59
  • The specific question, how to add a delay in a php script, is Google fodder. The core premise of this question duplicates the referenced question. I suggest you read the accepted answer there -- not sure how many emails you're dealing with, but there is a lot more to consider than a simple delay in between mails. – Chris Baker Jul 12 '14 at 07:00
  • 1
    Also read this one: http://stackoverflow.com/questions/1118154/sending-mass-email-using-php – Chris Baker Jul 12 '14 at 07:04
  • Unless your mail server is total junk, you won't be able to overload it from PHP. Mine copes happily with 2 million messages/hour. Mail servers are very good at queuing. – Synchro Jul 12 '14 at 07:06
  • Ah perfect guys, appreciate it. – Oliver Kucharzewski Jul 12 '14 at 07:13

1 Answers1

4

Just send to each user within loop, and after sending, use sleep() before next execution.

foreach($users as $user){
    $mail = new PHPMailer;
    // your settings, subject, body and so on here
    $mail->addAddress($user->email, $user->name); 
    $mail->send()
    sleep(5); // 5 seconds
}
Burak
  • 5,252
  • 3
  • 22
  • 30
  • 2
    Depending on the number of emails involved, [there is a lot more to it than just `sleep()`](http://stackoverflow.com/questions/3905734/how-to-send-100-000-emails-weekly/3905805#3905805) – Chris Baker Jul 12 '14 at 07:02