...

View Full Version : First record missing when sending out PHP mail



galahad3
04-19-2012, 02:15 PM
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>\n";
$headers .="MIME-Version: 1.0\n";
$headers .="Content-Type: multipart/mixed; boundary=\"".$mimebound."\"\n\n";
$headers .="This is a multi-part message in MIME format.\n\n";

while($row = mysql_fetch_array($result)){
$message .="--".$mimebound."\n";
$message .="Content-type:text/plain; charset=iso-8859-1\n";
$message .="Content-Transfer-Encoding: 7bit\n";
$message .="Content-Disposition: inline\n\n";
$message .="Enquiry from the website\n";
$message .="--------------------\n\n";
$message .= mysql_real_escape_string(@$_POST['_EmailText']) . "\n";
$message .="--\n\n";
mail($row['Email'], $subject, $message, $headers);
}

$count++ ;
}
echo '</table>';
?>

Fou-Lu
04-19-2012, 02:46 PM
You've two queries with the same result name and you are fetching twice in a "single" iteration.
Remove these three lines:


$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// get results
$result = mysql_query($query) or die("Couldn't execute query");

And replace them with


$result = mysql_query($query) or die("Couldn't execute query " . mysql_error());

And remove the nested while loop. The body of it can retained, just not the loop.
Dumped the count of records as well since its not used in here anywhere.

galahad3
04-19-2012, 03:10 PM
Thanks, that sort of works, but the odd thing I found when testing is that if an email is sent out, and then another email sent out to the same address, it repeats the message in the body of the email for some reason? It seems to add the message again for each time it sends an email to that address- so having tested with the same email addresses a few times I've found that they're getting the message repeated several times in the email...

This is the code I now have:



<?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>\n";
$headers .="MIME-Version: 1.0\n";
$headers .="Content-Type: multipart/mixed; boundary=\"".$mimebound."\"\n\n";
$headers .="This is a multi-part message in MIME format.\n\n";

$message .="--".$mimebound."\n";
$message .="Content-type:text/plain; charset=iso-8859-1\n";
$message .="Content-Transfer-Encoding: 7bit\n";
$message .="Content-Disposition: inline\n\n";
$message .="Enquiry from the website\n";
$message .="----------------\n\n";
$message .= $_POST['_EmailText'] . "\n";
$message .="--\n\n";
mail($row['Email'], $subject, $message, $headers);

}
echo '</table>';
?>

Fou-Lu
04-19-2012, 05:16 PM
Because $message has not been declared within the while loop, only appending is occurring. Add a $message = ""; within the first step of the loop. Same goes for any of these concatenated variables like $headers.

galahad3
04-19-2012, 05:28 PM
That's done the trick, emptying both headers and message. Thanks for your help with that. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum