Forum Moderators: coopster

Message Too Old, No Replies

PHP Sleep() Function

can I do this?

         

dreamcatcher

5:08 pm on May 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys,

I`ve got this little mailing list I`m setting up. Now if there are a lot of e-mails in the list, to send a message to all of them in one go would be a bit of a strain on the server. So I was wondering if I could add some kind of sleep() function so that an x amount of messages are sent, before there is a slight pause, say 3 seconds, before the next batch are sent.

What do you think? Any ideas?

I`m using a loop to send all the emails, such as:

while ($row = mysql_fetch_array($result))

{

$emailall = $row[0];
$message = stripslashes($message);
mail("$emailall", "$subject", "$message", "From: $email");

}

Thank you!

:)

DrDoc

5:11 pm on May 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can always go to PHP's Web site php.net and search...

A search there brings up the following page: sleep() [php.net] ;)

dreamcatcher

5:31 pm on May 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, I did that first, but I`m none the wiser. I mean, I can see that it will work, but I`m not sure of the syntax

:(

DrDoc

5:35 pm on May 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... // Some code here
sleep(1); // Script will halt for one second
... // More code here

Just put in sleep(n) wherever you want the script to hald. n = number of seconds

dreamcatcher

6:07 pm on May 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can see how sleep() works, but how do I add it to the code above. Lets say the user has specified that he wants 10 messages sent before the pause, this is done in the variable:

$send_delay = "10";

How then do I add that into the syntax?

:)

ruserious

8:50 pm on May 4, 2003 (gmt 0)

10+ Year Member



If you are going to send mass mails (i.e. 500 or more), the following things will help:
- If persinalization is not a definitive must, send one email with the recipients emails as BCC. This way it will not put as much strain on the server. Sending single emails is hard on the server
- Make sure you sort your email-adresses by domain. When you have all BCC recipients from one domain (which is not unusual for hotmail etc.) you mailserver only needs to connect one time to the destination server.
- Use set_time_limit(1200) to prevent the script from dying. Use ignore_user_abort(true) to prevent the script from stopping when the browser closes the connection.

dreamcatcher

10:25 pm on May 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the info ruserious, but that still doesn`t answer my question. How do I write the syntax for it to work?

ggrot

3:36 am on May 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$send_count = 10;
$send_delay = 1;

while ($row = mysql_fetch_array($result))
{
if($send_count == 0)
{
$send_count = 10;
sleep($send_delay);
}
$send_count--;

$emailall = $row[0];
$message = stripslashes($message);
mail("$emailall", "$subject", "$message", "From: $email");

}

That'll send 10 messages, then pause 1 second, then send 10 messages... you get the picture.

ggrot

3:38 am on May 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ruserious, Are you sure about that BCC thing? I would suspect that since the server still has to make sure a copy of the email gets sent to every person, it would take roughly the same order of time as sending out all the messages individually.

grahamstewart

4:01 am on May 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A slightly neater solution using the modulus operator...

[pre]
$between_delay = 10;
$send_delay = 1;

for ( $sent=0; $row = mysql_fetch_array($result); $sent++ ) {

if ( ($sent % $between_delay) == 0 )
sleep( $send_delay );

$emailall = $row[0];
$message = stripslashes($message);
mail("$emailall", "$subject", "$message", "From: $email");
}
[/pre]

dreamcatcher

7:25 am on May 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



grahamstewart and ggrot, thanks so much for your help.

Excellent!

:)

ruserious

2:00 pm on May 5, 2003 (gmt 0)

10+ Year Member



ruserious, Are you sure about that BCC thing? I would suspect that since the server still has to make sure a copy of the email gets sent to every person, it would take roughly the same order of time as sending out all the messages individually.

The difference is in putting them in the queue. If you send each email seperately with mail(), you're calling sendmail each and everytime to queue one single message. With one email and xxx BCCs you only have that overhead once.