Using the sleep function to throttle mail()
Posted: Fri Jul 30, 2010 1:54 am
Hi forum,
I have a cronjob trigger this script once a month. My webhost has an email limit set to 500/hr which I am soon to exceed, so I need the sleep function to delay the email output to 1 message every 8 seconds until all emails have been sent. It also needs to loop through each email pulled from the database based on the $query while it delays each message. I have managed to get it to where it will delay the messages 8 seconds apart, but it keeps sending the same email to the same user over and over.
Here is a simple example of my script.
The script and cronjob function perfectly as I have them currently. I just need to throttle the output so as not to exceed the 500/hr limit.
This is the only part of my project to give me any trouble and I have been searching for help and trying things out for two straight days. Any help would be greatly appreciated.
thanks a ton!!!
John
I have a cronjob trigger this script once a month. My webhost has an email limit set to 500/hr which I am soon to exceed, so I need the sleep function to delay the email output to 1 message every 8 seconds until all emails have been sent. It also needs to loop through each email pulled from the database based on the $query while it delays each message. I have managed to get it to where it will delay the messages 8 seconds apart, but it keeps sending the same email to the same user over and over.
Here is a simple example of my script.
Code: Select all
<?php
require('db_connect.php');
$query = "SELECT * FROM table WHERE status='x' AND form='xx'";
$result = mysql_query($query);
$num = mysql_numrows($result);
$i=0;
while ($i < $num)
{
$field1 = mysql_result($result,$i,"id");
$field2 = mysql_result($result,$i,"firstname");
$field2 = mysql_result($result,$i,"lastname");
$field10 = mysql_result($result,$i,"email");
$queryreg = mysql_query("INSERT INTO table VALUES ('$field1','$field2','$field3')");
$to = "$field10";
$from = "webmaster@mysite.com";
$subject = "Receipt";
$message = "xxx";
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";
$headers .= "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
$i++;
}
?>This is the only part of my project to give me any trouble and I have been searching for help and trying things out for two straight days. Any help would be greatly appreciated.
thanks a ton!!!
John