Using the sleep function to throttle mail()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
j p d
Forum Newbie
Posts: 3
Joined: Fri Jul 30, 2010 1:01 am

Using the sleep function to throttle mail()

Post by j p d »

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.

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++;
	}
	
		
?>
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
j p d
Forum Newbie
Posts: 3
Joined: Fri Jul 30, 2010 1:01 am

Re: Using the sleep function to throttle mail()

Post by j p d »

I have a few test emails set up and all emails go through properly, at the same time, using cronjob with the script as it appears above, however when i add the sleep function like below it will send 1 message 8 seconds apart, but the same message will get sent to one user over and over and nothing will get sent to the other users. I feel like I am close, maybe I just have some brackets in the wrong place???

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')");
     
$send = 1;
$delay = 8;
for ( $sent=0; $row = mysql_fetch_array($result); $sent++ )
{
if ( ($sent % $send) == 0 )
sleep( $delay );
       
        $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++;
        }
       
               
?>
j p d
Forum Newbie
Posts: 3
Joined: Fri Jul 30, 2010 1:01 am

Re: Using the sleep function to throttle mail()

Post by j p d »

Okay, after getting some sleep, I was able to get it working this morning!! In case anyone should ever search for information on how to do this, I will post my working code to throttle email output using the sleep function.

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);

$send = 1;
$delay = 8;

        $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')");
     


if ( ($sent % $send) == 0 )
{
sleep( $delay );
       
        $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++;
}
       }       
               
?>
Post Reply