I am making a mailing list, but it is timing out before all the mail is being sent out.
I have put it into a loop to make the email personal with the users name.
One part of the code I am unsure about is use of sleep(). I got this idea from a mailing list tutorial. I scoured the net for these after I found the initial problem and it was meant to stop the script from timing out, which I didn't think would work and sure enough, it didn't.
Any ideas on how I can overcome this? My code is below.
- Code: Select all
//Retrieve newsletter information
$sql = "SELECT * FROM newsletter WHERE newsletter_id = ".$_GET['send'];
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$months = $row['months'];
//Prepare newsletter string filename
if (strlen($_GET['send']) == 1) {
$id = "0".$_GET['send'];
} else {
$id = $_GET['send'];
}
//Read selected newsletter to variable
$filename = "../newsletter".$id.".htm";
if ($newsletter = file_get_contents($filename)) {
$mail = file_get_contents("newsletter.template.htm");
$mail = str_replace(array("{MONTHS}", "{NEWSLETTER}"), array($months, $newsletter), $mail);
$subject = "DFNZ ".$months." Newsletter";
//Prepare mail list
$sql = "SELECT * FROM mailinglist";
$result = mysql_query($sql) or die(mysql_error());
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Diamonds Forever NZ <jayne@dfnz.co.nz>\r\n";
$x = 0;
$hold = 30;
while ($row = mysql_fetch_assoc($result)) {
$sendmail = str_replace(array("{NAME}", "{EMAIL}", "{CODE}"), array(ucfirst(strtolower($row['name'])), $row['email'], $code), $mail);
if (!mail ($row['email'], $subject, $sendmail, $headers)) {
$unsent[] = $row['email'];
}
$x++;
if($x == $hold) { // When $x is equal to $hold, a 3 sec delay will occur avoiding php to timeout
sleep(3);
$x = 0;
}
}
echo "<p>The newsletter has been sent to your mailing list.</p>";
if (isset($unsent)) {
echo "<p><br>However, the following email addresses did not work</p><br>\n";
foreach ($unsent as $u) {
echo "<br>".$u['email']."\n";
}
}
$sql = "UPDATE newsletter SET sent = 'True', sent_date = UNIX_TIMESTAMP() WHERE newsletter_id = ".$_GET['send'];
mysql_query($sql) or die(mysql_error());
} else {
echo "<p>The newsletter selected can not be retrieved so nothing has been sent.</p>";
}



