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

Dani AI

Generated

This thread shows was iterating a resultset and using PHP's mail() while suggested collecting addresses then looping with foreach. The code in the original posts eventually worked, but a few common pitfalls can silently make a loop send only one effective message or report only one address. The notes below explain what to check and a more reliable approach.

Common causes and checks:

  • Header line endings: email headers must use CRLF (\r\n). Mixing PHP_EOL and \r\n or adding extra newlines can terminate headers early and confuse the MTA. Standardize on \r\n for header lines.
  • BCC vs individual sends: adding a BCC for each recipient while also addressing the message to them is redundant. Decide whether to send one message with a BCC list or send separate messages per recipient. Many deliverability issues disappear when sending individual messages via authenticated SMTP.
  • mail() return and logs: check the boolean return of mail() and your mail server logs for delivery errors. Spam filters and rate limits can block many recipients.
  • Reporting/counting: an echo after the loop only shows the last value — move reporting inside the loop or keep a counter. pointed this out earlier.
  • Upgrades: avoid mysql_* functions. Use mysqli or PDO and validate/clean email addresses before sending.

Recommended reliable pattern (use a mail library + SMTP). Example outline:

$pdo = new PDO(...);
$emails = $pdo->query('SELECT email FROM emails')->fetchAll(PDO::FETCH_COLUMN);
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
// configure SMTP/auth here
foreach ($emails as $to) {
  $mail->clearAddresses();
  $mail->addAddress($to);
  $mail->Subject = 'Website Update';
  $mail->msgHTML($message);
  $mail->send();
}

This approach improves deliverability, makes errors visible, and scales better than raw mail() calls.

Recommended Answers

All 5 Replies

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.