i am trying to get this code to work but there is something wrong with the array (not to good at these). the main part of the code works it is sending out the email but only ever to one registered user.

here is the code i have so far.

$query = "SELECT email FROM emails";
	$result = mysql_query($query) or die(mysql_error());

	while($row = mysql_fetch_array($result)) {
	
	$contactemail = $row['email'];
	



	// Contacts
	//$replyName = $merc_replyId;
	//$replyEmail = $merc_replyAddress;
	
	$replyName = "PiPonliine";
	$replyEmail = "info@piponline.info";

	$contactname = "";
	

	// Subject
	$subject = "Website Update";

	// Headers
	$headers = "MIME-Version: 1.0" . PHP_EOL;
	$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
	$headers .= "From: ".$replyName." <".$replyEmail.">" . PHP_EOL;
	$headers .= "BCC: ".$contactname." <".$contactemail.">\r\n" . PHP_EOL;
	

	mail($contactemail, $subject, $message, $headers);
	}
  echo "$contactemail";
  echo "<h2>Email notification sent</h2>";
  echo "<h2>1 record added</h2>";

  mysql_close($con);

i am trying to get this code to work but there is something wrong with the array (not to good at these). the main part of the code works it is sending out the email but only ever to one registered user.

Hi..
i dont know the exact solution....but, try to do this...

1) if there is any id in database, try to using id by incrementing.......
2) try to use FOREACH instead of while......


this is just try, i dont know the exact way.....

a foreach loop will need to start with a while loop and i do not see the need to use the id of the table as i only need to loop through the email address and user these to send the email.

@Kevin wood, Your script seems fine.
Try this.

$query = "SELECT email FROM emails";
	$result = mysql_query($query) or die(mysql_error());

	while($row = mysql_fetch_array($result)) {
	
	$emailaddress[] = $row['email'];
	}


	// Contacts
	//$replyName = $merc_replyId;
	//$replyEmail = $merc_replyAddress;
	$i=0;
	foreach($emailaddress as $contactemail) {
	$replyName = "PiPonliine";
	$replyEmail = "info@piponline.info";

	$contactname = "";
	

	// Subject
	$subject = "Website Update";

	// Headers
	$headers = "MIME-Version: 1.0" . PHP_EOL;
	$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
	$headers .= "From: ".$replyName." <".$replyEmail.">" . PHP_EOL;
	$headers .= "BCC: ".$contactname." <".$contactemail.">\r\n" . PHP_EOL;
	

	mail($contactemail, $subject, $message, $headers);
	$i++;
	}
  echo "$contactemail"; //this will print only 1 email address, which is the last one
  echo "<h2>Email notification sent</h2>";
  echo "<h2>".$i." mails sent</h2>";

This definitely works. (I tried with 3 email addresses)

commented: very helpfull +2

thank you for the reply i ended up getting it working late last night with a similar piece of code. thank you again for the reply.

You are welcome.. Cheers man!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.