tags:

views:

67

answers:

2

Forgive me for this noob question, but is there such a setting that sets a certain amount of time (e.g. milli/seconds) that has to pass in between sending emails through a script? How is that setting called and where do I change that setting?

I'll give an example: I used to have a PHP script that sends emails like so:

for ($i=0; $i<count($emails); $i++) {
     mail($email[$i],'test','test');
}

It turned out that not all emails were sent successfully because the script ran so fast that there was not enough time in between sending emails that was required by the server.

Did I make sense?

+4  A: 

You can use one of these functions to do nothing for a while :

  • sleep() : Delays the program execution for the given number of seconds.
  • usleep() : Delays program execution for the given number of micro seconds.


Putting one of those in your loop should help.
For example :

for ($i=0; $i<count($emails); $i++) {
    mail($email[$i],'test','test');
    usleep(100*1000); // 100 milli-seconds
}
Pascal MARTIN
How many milliseconds/seconds should be enough?I wouldn't want to set the sleep too high that the execution of my script would exceed the max_execution time :)
Obay
If I remember correctly, at least on Linux, `max_execution_time` counts the amount of time during which PHP is *working* ; when *sleeping*, PHP is not *working*, and the time spent with `sleep()` or `usleep()` should not be included in the `max_execution_time` limit ;;; how much time should be used, in your case ? Well, you'll have to try a couple of values, to find the right ones for your servers ;-) *(There is no value that will be OK in all situations, I'd say)*
Pascal MARTIN
oh great! i only need to send around 200 emails. i don't think that would exceed the time limit. thanks man!
Obay
You're welcome :-) Have fun !
Pascal MARTIN
+1  A: 

This script is untested but the theory is sound. Upon each send mail, check with a delay sequence to see if the mail has sent. I've set a limit to ensure the script does not fail - with the print, the execution time shouldn't be a problem.

for ($i=0; $i<count($emails); $i++) {
   $sent = mail($email[$i],'test','test');
    $count = 0;

   while($sent == false) {
    usleep(500); // half a second - test this number until the minimum is found
    $count++;

    if($count == 1000) {
        echo "Email to " . $email[$i] . " failed due to timeout</br>";
        break;
    }
   }
}

does that help?

Glycerine
great! :) but should usleep() be sleep(0.5) ? also, shouldn't the mail() be called again inside the while() ?
Obay
Well obviously if you want to be accurate about the code! Ha. my bad, I was thinking of actionscript and milliseconds usleep(500000) or sleep(.5) will both work. And I didn't want to put the mail() inside the while, as the while only ensures the previous email got sent before going "right ok, onto the next one" therefore creating a delay longer than half a second if required. The little counter and if statement is just a little defence to ensure the script doesn't endlessly loop. Effectively is pseudo for "send an email... am I done? am I done? am I done? am I do... Right NEXT ONE!"
Glycerine
thanks! I didn't know it's possible for the script to continue even though the mail() hasn't really finished yet, that's why i thought the mail() should again be put in the while. nice script :) thanks
Obay