Modify the script to send batches of 500 to be sent

My hosting allows 500 emails per hour. This script sends emails to groups which are more then 500 and thus has created a big problem for us. My coder and I can't think of the basic logic of how to break up the emails into batches of 500 so the host can que them. How can this be done?
Starquest321Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Starquest321Author Commented:

<?php
$is_cron_task_email = 1;  // Flag to mark whether the script is a cron task or not	
include("setting.php");
include("class.phpmailer.php");
#printf("9999999");

$db_connect = mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_name, $db_connect) || die(mysql_error());
#printf("888-->%d",$row_mailout);

 $sql_query				= "SELECT * FROM member_messages WHERE msg_status = 'pending' ORDER BY msg_date_created ASC LIMIT 0, $row_mailout";



$result					= mysql_query($sql_query) or die(mysql_error());
while($row				= mysql_fetch_array($result)) {

    $msg_id				= $row[msg_id];
    $msg_member		    = $row[msg_member];
    $msg_member_name	= $row[msg_member_name];
    $msg_member_group	= $row[msg_member_group];
    $msg_member_email	= $row[msg_member_email];
    $msg_subject		= $row[msg_subject];
    $msg_details		= $row[msg_details];
	$msg_memberid 		= $row['messages_memberid'];
	$msg_member_name	= $row['msg_member_name'];
	$attach1 			= $row['msg_Attachment1'];
	$attach2 			= $row['Msg_attachment2'];
	$attach3 			= $row['Msg_attachment3'];
	$attach4 			= $row['Msg_attachment4'];
	$attach5 			= $row['Msg_attachment5'];
	$attach6 			= $row['Msg_attachment6'];
	$attach7 			= $row['Msg_attachment7'];
	$attach8 			= $row['Msg_attachment8'];
	$attach9 			= $row['Msg_attachment9'];
	$attach10 			= $row['Msg_attachment10'];
	$attach1_name		= $row['msg_Attachment1_name'];
	$attach2_name		= $row['msg_Attachment2_name'];
	$attach3_name		= $row['msg_Attachment3_name'];
	$attach4_name		= $row['msg_Attachment4_name'];
	$attach5_name		= $row['msg_Attachment5_name'];
	$attach6_name		= $row['msg_Attachment6_name'];
	$attach7_name		= $row['msg_Attachment7_name'];
	$attach8_name		= $row['msg_Attachment8_name'];
	$attach9_name		= $row['msg_Attachment9_name'];
	$attach10_name		= $row['msg_Attachment10_name'];
	$attach1_type		= $row['msg_Attachment1_type'];
	$attach2_type		= $row['msg_Attachment2_type'];
	$attach3_type		= $row['msg_Attachment3_type'];
	$attach4_type		= $row['msg_Attachment4_type'];
	$attach5_type		= $row['msg_Attachment5_type'];
	$attach6_type		= $row['msg_Attachment6_type'];
	$attach7_type		= $row['msg_Attachment7_type'];
	$attach8_type		= $row['msg_Attachment8_type'];
	$attach9_type		= $row['msg_Attachment9_type'];
	$attach10_type		= $row['msg_Attachment10_type'];	
	
	if($msg_memberid != 0)
	{
		$sql_from = "";
		$sql_from = "select email from databaseuser where databaseid='$msg_memberid'";
		$qry_from = mysql_query($sql_from);
		$res_from = mysql_fetch_object($qry_from);
		$email_from = $res_from->email;
		$email_from_name = $msg_member_name;
		
	}


   if($attach1 != "")
   	{
		$fp="";
		$fname="";
		$fname = $msg_id."_".$attach1_name;
		$fp=fopen("temp/attachements/$fname","w");
		fwrite($fp,$attach1,strlen($attach1));
		fclose($fp);	
		
	}
	
	  if($attach2 != "")
   	{
		$fp="";
		$fname="";
		$fname = $msg_id."_".$attach2_name;
		$fp=fopen("temp/attachements/$fname","w");
		fwrite($fp,$attach2,strlen($attach2));
		fclose($fp);	
		
	}
	
	   if($attach3 != "")
   	{
		$fp="";
		$fname="";
		$fname = $msg_id."_".$attach3_name;		
		$fp=fopen("temp/attachements/$fname","w");
		fwrite($fp,$attach3,strlen($attach3));
		fclose($fp);	
		
	}
	
	   if($attach4 != "")
   	{
		$fp="";
		$fname="";
		$fname = $msg_id."_".$attach4_name;		
		$fp=fopen("temp/attachements/$fname","w");
		fwrite($fp,$attach4,strlen($attach4));
		fclose($fp);	
		
	}
	
	   if($attach5 != "")
   	{
		$fp="";
		$fname="";
		$fname = $msg_id."_".$attach5_name;		
		$fp=fopen("temp/attachements/$fname","w");
		fwrite($fp,$attach5,strlen($attach5));
		fclose($fp);	
		
	}
	
	   if($attach6 != "")
   	{
		$fp="";
		$fname="";
		$fname = $msg_id."_".$attach6_name;		
		$fp=fopen("temp/attachements/$fname","w");
		fwrite($fp,$attach6,strlen($attach6));
		fclose($fp);	
		
	}
	
	   if($attach7 != "")
   	{
		$fp="";
		$fname="";
		$fname = $msg_id."_".$attach7_name;		
		$fp=fopen("temp/attachements/$fname","w");
		fwrite($fp,$attach7,strlen($attach7));
		fclose($fp);	
		
	}

	   if($attach8 != "")
   	{
		$fp="";
		$fname="";
		$fname = $msg_id."_".$attach8_name;		
		$fp=fopen("temp/attachements/$fname","w");
		fwrite($fp,$attach8,strlen($attach8));
		fclose($fp);	
		
	}

	   if($attach9 != "")
   	{
		$fp="";
		$fname="";
		$fname = $msg_id."_".$attach9_name;		
		$fp=fopen("temp/attachements/$fname","w");
		fwrite($fp,$attach9,strlen($attach9));
		fclose($fp);	
		
	}

	   if($attach10 != "")
   	{
		$fp="";
		$fname="";
		$fname = $msg_id."_".$attach10_name;		
		$fp=fopen("temp/attachements/$fname","w");
		fwrite($fp,$attach10,strlen($attach10));
		fclose($fp);	
		
	}

#    printf("===%d===\n",$msg_id);
if($msg_member_group!='')
{

$groups_sel = array();
$groups_sel = explode(",",$msg_member_group);
$sql_mid=array();
foreach($groups_sel as $valz)
{
	$sql_mid[] = " (search_group LIKE '%$valz%') ";
}
$sql_stick = implode(" OR ",$sql_mid); //Modified the code to work with ,seperated values with same format

  $sq =  "select search_email from search_reminder where $sql_stick GROUP BY search_email";
   //if (strcmp("realtor",strtolower($msg_member_email))==0 || strcmp("investor",strtolower($msg_member_email))==0){
    $msg_member_email=trim($msg_member_email);
    if (!strstr($msg_member_email,"@"))
	{
        $res=mysql_query($sq);
     if(mysql_num_rows($res))
	 {
        while($rr=mysql_fetch_row($res))
		{
		
            $mail 				= new PHPMailer();
            $mail->Host 		= $smtp_server;
            $mail->SMTPAuth 	= $smtp_auth;
            $mail->Username 	= $smtp_username;
            $mail->Password 	= $smtp_password;

            $mail->From 		= $email_from;
            $mail->FromName 	= $email_from_name;
            $mail->WordWrap 	= $email_wordwrap;

			if($attach1!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach1_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}

			if($attach2!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach2_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}


			if($attach3!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach3_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}


			if($attach4!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach4_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}


			if($attach5!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach5_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}


			if($attach6!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach6_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}


			if($attach7!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach7_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}


			if($attach8!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach8_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}

			if($attach9!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach9_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}
			
			if($attach10!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach10_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}

 print "sending email to {$rr[0]} ... <br>";

            $mail->ClearAddresses();
            $mail->AddAddress($rr[0]);
            $mail->IsHTML(true);

            $mail->Subject 		= $msg_subject;
            $mail->Body    		= $msg_details;
			
           $mail->Send();					
        }
	 }
	}
	
}
else
{
	         $mail 				= new PHPMailer();
            $mail->Host 		= $smtp_server;
            $mail->SMTPAuth 	= $smtp_auth;
            $mail->Username 	= $smtp_username;
            $mail->Password 	= $smtp_password;

            $mail->From 		= $email_from;
            $mail->FromName 	= $email_from_name;
            $mail->WordWrap 	= $email_wordwrap;

			if($attach1!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach1_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}

			if($attach2!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach2_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}


			if($attach3!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach3_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}


			if($attach4!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach4_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}


			if($attach5!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach5_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}


			if($attach6!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach6_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}


			if($attach7!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach7_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}


			if($attach8!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach8_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}

			if($attach9!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach9_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}
			
			if($attach10!="")
			{
				$fname="";
				$fname = $msg_id."_".$attach10_name;		
				$filen="";
				$filen="temp/attachements/$fname";
				$mail->AddAttachment($filen);
			}



            $mail->ClearAddresses();
            $mail->AddAddress($msg_member_email);
            $mail->IsHTML(true);

            $mail->Subject 		= $msg_subject;
            $mail->Body    		= $msg_details;
			
            $mail->Send();					
	
}

    $sql_query 			= "
        UPDATE member_messages SET
        msg_status 			= 'sent',
        msg_date_sent 		= '$date_database'
        WHERE msg_id 		= '$msg_id'
        ";
    mysql_query($sql_query) or die(mysql_error());
    print "sending email to $msg_member_email ... <br>";


}
mysql_close($db_connect);
?>

Open in new window

0
Ray PaseurCommented:
Wow - over 400 lines of code?  Doubt if I have time to read that, but I will try to give you my take on this situation from a high-level perspective.

Think about what your host is saying.  500 emails per hour = 12,000 emails per day.  That is a ridiculously low limit for anyone in a consumer business.  A good solution is to find a different host!  You might consider a dedicated hosting plan or try SliceHost, for example.

Here is what I would do (and have done): http://www.constantcontact.com/index.jsp

If you want to program this yourself, and keep the current lame host, consider this design pattern that would probably work OK, or at least form a foundation for something that would work.

1. You will have two tables - one of messages, one of recipients. When a message is added to the message table, it will be marked "unsent" and the recipients table will be populated by adding all of your target clients to that table.  You might keep the message ID with the clients.  There are a lot of ways this can be linked up.

2. Set up a group of CRON jobs - they can all be the same script.  Run them at specific intervals, perhaps once an hour.  Each script does the following...

A. SELECT from messages WHERE status = unsent LIMIT 1.
B. SELECT from recipients LIMIT 450 ( or something similar to allow for occasional testing without going over the 500 limit ).
C. Send the message to each of the recipients and DELETE from recipients as the message is sent.
D. If the remaining count in the recipients table is zero, UPDATE messages to set status = sent.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.