Issue with PHP email form not picking up first record in table

Have a strange issue when sending out mail from a simple form with a PHP

mail handler behind it. It grabs the email addresses from a field called

“Email” in the mySQL db.

If I have say five email addresses (five records) in the table, it will

send out emails to addresses 2 to 5 but not 1 (i.e not the first record in

the table)

If I have three, it will send out to 2 and 3, but again not 1.

if I have just one, it doesn’t send out the email.

It seems to just ignore the first record in the table but I can’t see

anything in the code that would make it do this.

This is the code, anyone know what the problem might be?


<?php
include ('../inc/dbconnect.php');

$emailtext = mysql_real_escape_string(@$_POST['_EmailText']);

// 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");

// 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.\
\
";
	
	while($row = mysql_fetch_array($result)){
    		$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 .= mysql_real_escape_string(@$_POST

['_EmailText']) . "\
";
			$message .="--\
\
";
         mail($row['Email'], $subject, $message, $headers);
        }
		
$count++ ;
}
echo '</table>';
?>

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);

Although you don’t seem to use $numrows anywhere.

Remove the first while and see if it will work.

Edit: didn’t see the above when I posted.

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>';
?>

Seems to be fixed now. Thanks again for the help. :slight_smile: