This is a script for sending out newsletters from a PHP script. Everything works fine except it doesn't reload the page to complete the mailing. It is supposed to send newsletters out in blocks of subscribers ($perpage). I have tested it and it stops after the number in $perpage and doesn't finish the rest of the list. It is supposed to reload the page and grab the next block of subscribers, send the newsletter to them and so on.

The following script fails to reload the page after the number in $perpage. What am I doing wrong?

TIA from a desperate newbie

PHP Code:

<?php

ini_set
('max_execution_time''300'); // sets timeout to 5 minutes



$perpage 3// number of users to e-mail every time
if (!is_int($startnum)) {
    
$startnum 0;
}

echo 
'Emailing ' $perpage ' users, starting at record ' $startnum+.'...';

$subscribers mysql_query("SELECT email, newsletter_format FROM newsletter WHERE validation = 1 LIMIT $startnum,$perpage");
if (!
$subscribers) {
  echo(
"<p>Error retrieving subscriber list from database!<br />".
       
"Error: " mysql_error() . "</p>");
  exit();
}
while (
$subscriber mysql_fetch_array($subscribers)) {
    
$to htmlspecialchars($subscriber['email']);
    
$newsletter_format htmlspecialchars($subscriber['newsletter_format']);
    if (
$newsletter_format == 'html') {
        
mail($to$subject$htmlmessage$htmlheader);
        
$htmlsent++;
    } elseif (
$newsletter_format == 'text') {
        
mail($to$subject$textmessage$textheader);
        
$textsent++;
    }
}

echo 
'Done! Taking you to next page, please hold...';
echo 
"\n<script language=\"javascript\">\n";
echo 
"<!--\n";
echo 
"window.location=\"" $PHP_SELF "?startnum=" $startnum+$perpage "\";\n";
echo 
"// -->\n";
echo 
"</script>\n";
//echo ("<h1>Newsletter # $issue Sent</h1>");
?>
<p><a href="admin.php"><img src='images/main-console.gif' width='114' height='51' border='0' alt='Go back to the main console'></a><a href="newnewsletter.php"><img src='images/new-newsletter.gif' width='114' height='51' border='0' alt='Create a new newsletter'></a><a href="newsletters.php"><img src='images/show-newsletters.gif' width='114' height='51' border='0' alt='Go back to the newsletter management console'></a><br><br></p>
<?php

$sent_to 
= ($htmlsent $textsent);
$sql "UPDATE newsletter_management SET sent_to='$sent_to', send_date = curdate(), sent='yes' WHERE ID=$id";
$newslettersent = @mysql_query($sql);
if (!
$newslettersent) {
  echo(
"<p>Error updating newsletter_management table!<br />".
       
"Error: " mysql_error() . "</p>");
  exit();
}
echo (
"<h3>Newsletter # $issue sent to $htmlsent html subscribers and $textsent text subscribers</h3>");
echo (
"<h3>A total of $sent_to subscribers</h3>");


?>