Jump to content

While Loop is sending multiple Emails


kmadden84

Recommended Posts

Hello everyone.

 

I almost have this down, and I think the problem is how I'm using the while loop.

 

The below script is meant to query my database and pull serial numbers based on email address.  The serial numbers found are then sent to the email address. 

 

Using the script below - I get multiple emails.  E.g. if there's 100 serial numbers, I will get 100 emails.  The first email has 1 serial, the second has 2, the third 3, etc etc until it reaches 100.  The "Email being sent" message is also repeated 100 times.  Am I not closing the while loop?

 


<?php


   $db = mysql_connect("localhost","user","pass");
		if(!$db) die("Error connecting to MySQL database.");
		mysql_select_db("database" ,$db);

$to = $_POST['formEmail'];
$varEmail = $_POST['formEmail'];
$varName =  $_POST['formName'];                      
$email = "information@whatever"; 
        


$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  
$data = mysql_query("SELECT Serial FROM SerialRecord WHERE Email = '$varEmail'");
if(mysql_num_rows($data) > 0)
while($info = mysql_fetch_array( $data ))
{
$subject = "Serial Number";     
$message.="<table border='1'>";
$message.="<tr>";
$message.= "<td>" . $info['Serial'] . "</td>";
$message.="</tr>";
$message.="</table>";
mysql_close($db);{}

mail($to,$subject,$message,$headers);


?>
<p class="auto-style12">Thank you for your inquiry <i><b><?php echo "$varName"; ?></b></i>. Your 
email has been sent to <?php echo "$varEmail"; ?> You should receive an email within the next few minutes containing your serial number</p> 
  <?php	} 


else
{ ?>

<p class="auto-style12">Sorry, the email address <i><b><?php echo "$varEmail"; ?></b></i> 
was not found.  Please contact <a href="mailto:support@whatever>Support</a> for further assistance. <br></fp>
<?php	} 



?>



Link to comment
https://forums.phpfreaks.com/topic/265967-while-loop-is-sending-multiple-emails/
Share on other sites

Figured it out myself.  Working code for anyone interested.

 

 <?php


   $db = mysql_connect("localhost","user","pass");
		if(!$db) die("Error connecting to MySQL database.");
		mysql_select_db("database" ,$db);

if($_POST['formSubmit'] == "Submit")
$varEmail = $_POST['formEmail'];
$varName =  $_POST['formName'];                      
$to = $_POST['formEmail'];
$email = "[email protected]"; 
$subject = "Serial Number";             
$body.= "Hello $varName. Your Serial Numbers are:<BR/><BR/>"; 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  
$data = mysql_query("SELECT Serial FROM SerialRecord WHERE Email = '$varEmail'");
if(mysql_num_rows($data) > 0){ 
?>
<font size="2" face="Arial, Helvetica, sans-serif">Email found sending now.<br></font>
<?php
while($info = mysql_fetch_array( $data ))
$body.= "<td>" . $info['Serial'] . "</td><BR />";
$body.="</tr>";
$body.="</table>";
mail($to,$subject,$body,$headers); 
}
else
{ 
?>
<font size="2" face="Arial, Helvetica, sans-serif">Sorry, the Email Address<i><b><?php echo "$varSerial"; ?></b></i> 
was not found.  Please contact <a href="mailto:[email protected]">Support</a> for further assistance. <br></font>
<?php	
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.