Php email newsletter

I have a newsletter that needs to be sent out to my organization. Unfortunately, I am limited to 200 emails per hour with a database of over 200 people; I do not have access to a cron job.

I have this email script that I cobbled together that would send out 20 emails each time the page is refreshed. My problem is that it this email is only sending out only 1 email (the last email listed in the database at the limit)

I understand that my code is not great, but if you have any suggestions on how to make this work, I would appreciate it.


<?php

$tlim = $_GET['x'];
$x = $tlim;
$x2 = $tlim +20;
$limit = "$tlim,$x2";


require("c:\\php\\includes\\class.phpmailer.php");



$mail = new PHPMailer();



////////////////////////////////////////////////////////////////

// Customize the following 5 lines with your own information. //

////////////////////////////////////////////////////////////////




$mailhost = "mailhost";  //Change this to your actual Domain name.


$fromaddress = "mail email";  //Change this to the email address you will use to send and authenticate with.

$frompwd = "password";  //Change this to the above email addresses password.




$sql = mysql_query("SELECT * FROM email_newsletter LIMIT $limit")
or die(mysql_error());  

while($row = mysql_fetch_array( $sql )) {

$idname = $row['id'];

$lastname = $row['last'];

$firstname = $row['first'];

$emailname = $row['email'];

$membername = $row['membership'];


$fromname = $fromaddress;
$website = "website";
$attachment = "<a href=''>newsletter</a>";

$subject = "Subject Header";

$body = "Dear $firstname $lastname,<br /><br />
The Newsletter is available for upload. <br /><br />
To view, please click on the following link:<br /><br />
$attachment<br /><br /><br />

Thank you,<br />
Newsletter<br /><br /><br />

";




$mail->IsSMTP();

$mail->Host = $mailhost;

$mail->SMTPAuth = true;

$mail->Username = $fromaddress;

$mail->Password = $frompwd;

$toaddress = $emailname;

$mail->From = $fromaddress;

$mail->FromName = $fromname;

$mail->AddBCC($toaddress);


$mail->IsHTML(true);

$mail->Subject = $subject;

$mail->Body = $body;

   }





if(!$mail->Send())

{

   echo "Message could not be sent. <p>";

   echo "Mailer Error: " . $mail->ErrorInfo;

   exit;
}


$time = $_GET['time'];
?>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Corey (638@gohip.com ) -->
<!-- Web Site:   http://six38.tripod.com -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
var start=new Date();
	start=Date.parse(start)/1000;
	var counts=30;
	function CountDown(){
		var now=new Date();
		now=Date.parse(now)/1000;
		var x=parseInt(counts-(now-start),10);
		if(document.form1){document.form1.clock.value = x;}
		if(x>0){
			timerID=setTimeout("CountDown()", 100)
		}else{

			location.href="sendmail.php?send=true&time=<?php echo $time+1;?>&x=<?php echo $tlim+20;?>"
		}
	}
//  End -->
</script>
<SCRIPT LANGUAGE="JavaScript">
<!--
window.setTimeout('CountDown()',100);
-->




var sec = 30;   // set the seconds
var min = 00;   // set the minutes

function countDown() {
  sec--;
  if (sec == -01) {
    sec = 59;
    min = min - 1;
  } else {
   min = min;
  }
if (sec<=9) { sec = "0" + sec; }
  time = (min<=9 ? "0" + min : min) + " min and " + sec + " sec ";
if (document.getElementById) { theTime.innerHTML = time; }
  SD=window.setTimeout("countDown();", 1000);
if (min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  countDown();
});

</script>

</HEAD>

<BODY>

<SCRIPT LANGUAGE="JavaScript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
// Cache-busting LUBID bug.
var ran = Math.round(Math.random() * 899999) + 100000;
var lubid_string = "<img src=\\"http://lubid.lycos.com/one.asp?site=members.tripod.lycos.com&ord=" + ran + "\\" height=\\"1\\" width=\\"1\\">";
document.write(lubid_string);
//
//  End -->
</script>
</center>
You are being redirected in 

<span id="theTime" class="timeClass"></span><br /><br />

<FORM NAME="form1">

<INPUT TYPE="hidden" NAME="clock" SIZE="4" VALUE="10">

</FORM>
</center>


<br />
<br />
<?php 

$timemail = $time * 20;


$tm_sql = mysql_query("SELECT id FROM email_newsletter ORDER BY id DESC LIMIT 1");

 list($totaltimemail) = mysql_fetch_array( $tm_sql );


echo "$timemail mails of $totaltimemail have been sent.";

?>


</body>


move the sendmail statements within the db loop

eg, copy the mail->Send() to right after $mail->Body = $body;

Thanks, that cure one problem…but now I seem to have another problem.

it seems that this code is sending duplicate emails, not to mention that when I check those emails, it seems to be adding emails to the CC location

The examples below show what I see in progressive emails.

e.g.
To: a@name.com

To: a@name.com, b@name.com

To: a@name.com, b@name.com, c@name.com

How can I go about stopping this from occurring and allowing my code to function the way it should?

on a side note…how do I go about being able to add an attachment?

$mail->AddBCC($toaddress);

You’re adding them all as BCC. So either move send back out of the loop (and it should send one mail BCC to everyone), or don’t add BCC and send one email per person.

from what I understand, it is supposed to send out all 20 emails called from the database in one shot- but the way it is sending them out is incorrect.

What you are proposing is to have the mail set up the way I had it before?

The problem I have is that I either get the very last email sent…or it tries to send them by progressively adding them to the name.

(I have them as BCC at the moment only to allow privacy of emails. and have changed it for testing to addCC.)

I am looking to send out 20 emails per shot every x amount of minutes. so…I am not really sure what you meant in your post to help me achieve that without making it end up the way I had it previously- which still didn’t send properly.

Could you explain further?

If you add a BCC to an email, then the email goes to the recipient, AND the BCC address. Two emails are sent from the server.

So you can either NOT add BCC and just send emails to each recipient, OR add the addresses as BCC. The difference is that you will ->send 20 times in the loop, or ->send 1 time outside the loop.

ok, I understand that…it makes sense. But, I am only sending to the BCC.

I have tried doing this a few different ways…including adding a dummyname to the TO location.

Unfortunately I get similar problems…where the names are still tallied up progressively or it only takes the last person in the database limit.

I do understand what you are saying…I just do not think I am implementing it correctly to achieve my goal.

Well that is quite a bizarre problem :slight_smile: You are saying that BCC isn’t working, and BCC is working at the same time. You can try this in place of bcc.

$mail->ClearAddresses();
$mail->AddAddress($toaddress);
$mail->Send();

Thanks, that seemed to do the trick.

I really appreciate it.