Hi,
I have a script for sending an email out to addresses that i have in a mysql table in my database. Its working within a while loop so that one person gets a mail then its send to the next in the table.
Heres the code
$conn = mysql_connect("host", "name", "password")
or die(mysql_error());
mysql_select_db("table_name",$conn) or die(mysql_error());
$display_block = "";
require("class.phpmailer.php");
$sql="
SELECT *
FROM emails
";
$result = mysql_query($sql, $conn)or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
//give a name to the fields
$email=$row['email'];
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "localhost"; // SMTP server
$mail->From = "postmaster@mysite.com";
$mail->AddAddress("me@me.com");
//$mail->AddAddress("$email\n");
$mail->AddEmbeddedImage("images/cover17.jpg", "my", "cover17.jpg");
$mail->IsHTML(true);
$mail->Subject = "Newsletter";
$mail->Body = 'Embedded Image: <img src="cid:my" alt="cover" >';
$mail->Body = "
<table width=\"500\" border=\"1\" align=\"center\" cellpadding=\"3\" cellspacing=\"4\" bordercolor=\"#CCCCCC\" bgcolor=\"#FFFFFF\">
<tr><td colspan=\"2\" align=\"left\"><a href=\"http://www.mysite.com\"><img src=\"cid:my\" alt=\"cover\" ></a></td></tr>
<tr><td>Welcome etc.<br>
See it here <a href=\"http://www.mysite.com\">www.mysite.com</a></td></tr>
</table>";
$mail->WordWrap = 50;
if(!$mail->Send())
{
echo "Message was not sent";
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
$display_block = "Mail sent with picture to $email\n";
}
$email="";
}
At the moment i have it just sending to me because i'm worried that it will send all of the names in the table (about 500) 500 emails each, as it is doing this when i am sending tests to myself. I have had this problem before and would like to find a way to prevent this from happening.
I have cleared the variable $email after each loop but i'm still not convinced.