guymclaren Posted March 10, 2010 Share Posted March 10, 2010 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 />";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/194736-looping-problem/ Share on other sites More sharing options...
awpti Posted March 10, 2010 Share Posted March 10, 2010 Looks like you're running into the max_execution time. Bump it up or make your list pulls smaller. Quote Link to comment https://forums.phpfreaks.com/topic/194736-looping-problem/#findComment-1024084 Share on other sites More sharing options...
Psycho Posted March 10, 2010 Share Posted March 10, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194736-looping-problem/#findComment-1024086 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.