Jump to content

Looping problem


guymclaren

Recommended Posts

Can someone have a look at this for me please. I have a problem, some people are receiving this multiple times and there are no duplicates in the database, but mostly the script times out before completeing. I expect the script to echo every 100th or so record. It does not.  I have included the full script broken up into code blocks with notes. please help.

 

<?php
// Connection details MySQL server
$username="xxxxx";
$password="xxxxx";
$database="xxxxxx";
$server="xxxxx";
mysql_connect($server,$username,$password);
@mysql_select_db($database) or die("Unable to select database");
include ("../navigation/header.php");
?>

<div id="content">
<div id="contentleft">
<div class="post">
<div class="post-title">
<h2> Mailing List</h2>
</div>
<div class="post-entry">
<?

$sqlm="SELECT * FROM xxxxxx WHERE sent !='6' AND subs !='9' ORDER BY id ASC" ;
$objRSm = mysql_query($sqlm);
$num_rows = mysql_num_rows($$objRSm);

loop starts here


while($row = mysql_fetch_array($objRSm, MYSQL_ASSOC)) {
$pid = $row['id'];
$name = $row['name'];
$tomail = $row['email'];
$city = $row['City'];


$upd = "UPDATE maillist SET sent='6' WHERE email='".$tomail."'";
mysql_query($upd);

// From email address
$from = "xxxxxx";
$from_name = "xxxxxx ";

// The message
$subject = $name." THANK YOU"  ;

$message = "Hi ".$name.",\r\n"
."\r\n To unsubscribe click this link - http://xxxxxx.za/findit/unsub.php?id=".$tomail." Do not reply to this email, It was sent from an email address that does not get responded to. When you unsubscribe using the link, you will be removed from our mailing list immediately";


$message_html = "<html><body><p>Hi ".$name.",<br />"
."<a href=http://bit.ly/dgzjHA>33% off offer</a><br /><br />This message was sent from http://xxxxx.co.za, To unsubscribe click <a href=http://xxxxx.co.za/findit/unsub.php?id=".$tomail."> http://xxxxx.co.za/findit/unsub.php?id=".$tomail." </a>Do not reply to this email, It was sent from an email address that does not get responded to. When you unsubscribe using the link, you will be removed from our mailing list immediately </body>";



/***********************************************/
/* No need to modify anything down here */
/* Note that these are needed to send the mail */
/***********************************************/
// Generate text + html version
$random_hash = md5(date("r", time()));

$mailmessage = "
--PHP-alt-".$random_hash."
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$message

--PHP-alt-".$random_hash."
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$message_html

--PHP-alt-".$random_hash."--
";

// Headers
// To send HTML mail, the Content-type header must be set
$headers = "From: ".$from_name." <".$from.">" . "\r\n";
$headers .= "Reply-To: ".$from_name." <".$from.">" . "\r\n";
$headers .= "Date: ".date("r") . "\r\n";

// Additional headers
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-" . $random_hash . "\"\r\n";
$headers .= "Message-Id: <" . md5(uniqid(microtime())) . "@" . $_SERVER["SERVER_NAME"] . ">\r\n";

At this point I expect the echo on screen, but nothing happens and the script times out, I have to rerun the script about 4 times to do 1600 emails, How can I fix this?

// Send the mail
if ($pid % 100 == 0) {
echo $pid." has been reached";}
usleep(1200000);
mail($tomail, $subject, $mailmessage, $headers);

echo "The Newsletter has been sent to ".$num_rows." recipients<br />";}
?>	 

 

Link to comment
Share on other sites

One thing you can do to really improve the performace of this script would be to remove everything from within the loop that does not need to be there.

 

For example, there is no need to redefine these variables with the same values on every iteration of the loop

// From email address
$from = "xxxxxx";
$from_name = "xxxxxx ";

Define them before the loop.

 

But, what will help the most will be to remove the UPDATE query from within the loop. Instead save the $tomail variables to an array and then run a single update query at the end. In fact you should only mark a record as processed if the mail sends without error. Something like this:

$processed = array();
while($row = mysql_fetch_array($objRSm, MYSQL_ASSOC)) {
    //Do some stuff
    if (mail($tomail, $subject, $mailmessage, $headers))
    {
        $processed[] = $tomail;
    }
    //Do some more stuff
} //end loop

//Single update query for all processed records
$upd = "UPDATE maillist SET sent='6' WHERE email IN ('" . implode("', '", $processed) . "')";

 

As for echoing every 100 records to the page, you are apparently using the id field as the counter. Not sure what data you actually have in your database, but it is perfectly possible that you don't have the id numbers that you think you do.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.