You have two while loops on the same query result set, one inside the other. Why?
And also, you don’t need to run the query twice just because you want to get the number of rows. Instead of
// Build SQL Query
$query = "select * from emailcontacts ORDER BY Email ASC"; // specify the table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// get results
$result = mysql_query($query) or die("Couldn't execute query");
you can do
// Build SQL Query
$query = "select * from emailcontacts ORDER BY Email ASC"; // specify the table and field names for the SQL query
// get results
$result = mysql_query($query) or die("Couldn't execute query");
$numrows=mysql_num_rows($result);
I’ve changed and simplified the code so it’s now as follows. It sends out all the emails, i.e to all recoirds- BUT when I repeat the process, it seems to send extra copies of the message in subsequent emails, so if for example I send it out three times, the recipients get three copies of the message in the same email. Any ideas?
<?php
include ('../inc/dbconnect.php');
$emailtext = $_POST['_EmailText'];
// Build SQL Query
$query = "select * from emailcontacts ORDER BY Email ASC"; // specify the table and field names for the SQL query
$result = mysql_query($query) or die("Couldn't execute query " . mysql_error());
// display the results returned
while ($row= mysql_fetch_array($result)) {
$emailaddress = $row["Email"];
$id = $row["ID"];
$mailto = $emailaddress;
$mimebound = md5(uniqid(time()));
$subject = "Email from Emailer Function";
$headers .="From: Emailer <sales@mysite.com>\
";
$headers .="MIME-Version: 1.0\
";
$headers .="Content-Type: multipart/mixed; boundary=\\"".$mimebound."\\"\
\
";
$headers .="This is a multi-part message in MIME format.\
\
";
$message .="--".$mimebound."\
";
$message .="Content-type:text/plain; charset=iso-8859-1\
";
$message .="Content-Transfer-Encoding: 7bit\
";
$message .="Content-Disposition: inline\
\
";
$message .="Enquiry from the website\
";
$message .="-----------------------------------------------\
\
";
$message .= $_POST['_EmailText'] . "\
";
$message .="--\
\
";
mail($row['Email'], $subject, $message, $headers);
}
//echo '</table>';
?>