i am using the code below to send emails out to a mailing list. its takes 2 emails(right now, probably 50 when im done) from the database and sends out and email. then it submits a hidden form to the same page after it sends two emails. it then grabs the next 2 and sends those out. it does this till all emails are sent out. so basically it sends 2 emails, submits hidden form back to the same page, then send 2 more emails out. it does this till all emails are sent.
my problem is that while the script is executing none of the html is output and visible in the browser until the $mailer>send() is done. and when it appears it only appears for 1 sec then disappears.
i have tried all kinds of things like output buffering, sleep(), and stuff like that but it still does not work. can ANYONE PLEASE help me figure this out? ive been at it for a few days now.
HERE IS THE CODE
<?php
include "header.php";
$dbhost = 'localhost';
$dbusername = 'user';
$dbpasswd = 'pw';
$database_name = 'mydb';
if (!$action) {
echo"<b>Send Newsletter</b><br><br>\n";
echo "<span class=\"headers\">Send message to Mailinglist</span><br>\n";
echo"<form id=\"form1\" name=\"form1\" method=\"post\" action=\"mailtester.php?action=send_newsletter\">
<table width=\"90%\" border=\"0\" cellspacing=\"2\" cellpadding=\"2\">
<tr>
<td width=\"100\" align=\"right\" valign=\"top\">Subject:</td>
<td width=\"450\" align=\"left\" valign=\"top\"><input type=\"text\" name=\"subject\" /></td>
</tr>
<tr>
<td width=\"100\" align=\"right\" valign=\"top\">Message:</td>
<td width=\"450\" align=\"left\" valign=\"top\"><textarea name=\"body\" style=\"width: 100%; height: 450px;\" wrap=\"VIRTUAL\" id=\"txt\"></textarea></td>
</tr>
<tr>
<td width=\"100\" align=\"right\" valign=\"top\"> </td>
<td width=\"450\" align=\"right\" valign=\"top\">
<input type=\"submit\" name=\"Submit\" value=\"Submit\" /></td>
</tr>
</table>
</form>";
}else{
if ($action == "send_newsletter") {
$counter=$_POST['counter'];
$count=0;
$count2=0;
require ("class.phpmailer.php");
$mail = new phpmailer;
$subject=stripslashes(urldecode($subject));
$body=stripslashes(urldecode($body));
$mail->From = "me@me.com";
$mail->FromName = "me";
# $mail->WordWrap = 75;
$mail->UseMSMailHeaders = true;
$mail->AddCustomHeader("X-Mailer: mailer - Email Interface");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
echo "<b>Send Newsletter</b><br><br>\n";
$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")
or die ("Couldn't connect to server.");
$db = mysql_select_db("$database_name", $connection)
or die("Couldn't select database.");
$count=0;
echo " <table align=\"center\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\" width=\"100%\">\n";
if (!$counter){
$counter=0;
}
$count+=$counter;
//GET USER EMAILS IN CHUNKS OF 25, STARTING AT TALLY #//
$query = "SELECT * FROM mailinglist LIMIT $count,2";
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
//GET TOTAL NUMBER OF EMAILS NEEDED TO BE SEND
while ($db = mysql_fetch_array($result)) {
if (! get_cfg_var('safe_mode')) {set_time_limit(0);}
$mail->AddAddress("$db[email]", "");
if(!$mail->Send()) {
echo " <tr>\n";
echo " <td class=\"class3\">\n";
echo "<b> There was an error sending the message to $to !!!</b>";
echo " </td>\n";
echo " </tr>\n";
} else {
echo " <tr>\n";
echo " <td class=\"class3\">\n";
echo " <small>Mail to ".$db['email']." with subject: $subject -> <b>sent</b> !</small>";
echo " </td>\n";
echo " </tr>\n";
}
$mail->ClearAllRecipients();
$count++;
$count2++;
}
echo "</table>\n";
echo "<br>$count Newsletters sent.<br>\n";
mysql_close;
if ($count2==2) {
$redirect="newsletter";
echo "<small>... sending in progress - please be patient ...</small>";
} else {
echo "DONE";
}
if ($redirect=="newsletter") {
echo "
<form method=\"POST\" name=\"form\" id=\"form\" action=\"mailtester.php\">
<input type=hidden name=\"action\" value=\"send_newsletter\">
<input type=hidden name=\"counter\" value=\"$count\">
<input type=hidden name=\"subject\" value=\"".urlencode($subject)."\">
<input type=hidden name=\"body\" value=\"".urlencode($body)."\">
</form>
<script language=javascript>document.form.submit();</script>";
}
}//end if send newsletter
}
include "footer.php";
?>