Link to home
Start Free TrialLog in
Avatar of Starquest321
Starquest321

asked on

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?
Avatar of Starquest321
Starquest321

ASKER


<?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

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial