I am trying to plot my way through coding a BCC Bulk HTML email.

I can't get the code below to work and I want to also call another query to grab the emails for a BCC email. SPAM security would be helpful if it is easy enough to add tot he code.

<?
$Vol_Name = $_POST['Name'];
$id_Event = $_POST['id_Event'];
$Note = $_POST['Note'];
$sDate = $_POST['sDate'];

	require_once('auth.php');
	include("inc/interface.php");
	include("inc/funct.php");
	ServerConnect();
	DB_Connect();

// Adds a NOTE to a table
$sql="INSERT INTO Ev_Notes (id_Event, Name, Note) VALUES ('$id_Event','$Vol_Name','$Note')";
print_r($sql);
$result = mysql_query($sql) or die(mysql_error());

// Get all the Emails to include for a BBC bulk email
<?php
$query = "select Email FROM Ev_SchVolunteers  where id_Event='$id_Event' ORDER BY id_Sort, Name";
$results = mysql_query($query) or die("Error performing query"); 
?>

// NOT SURE WHAT DO TO TO HANDLE THE EMAIL ARRAY to GET IT INTO A FORMAT TO PASS TO THE VARIABLE $BCC that the EMAIL ACTION CAN HANDLE.

//Set Variables for the Email
$from = "me@web.net";
$to = "you@web.net";
$BBC = "????";
$subject = "New Note regarding the Event Date - $sDate"; // Why can't this pick up my Variable?
$message = 
" 
<html>
<body>
This new note is from: $sName.<br />
  <br />
  $Note<br />
  <br />
  <br />

  Please do not reply directly to this message from your email program, but go<br />
back click the link below and log-in to the THE SITE:<br />
<br />
<strong><a href="http://www.web.com/VOH/login-form.php">THE SITE</a></strong>
</body>
</html>
"; // I can't get the body of the email to format in HTML

send_email($from, $to, $subject, $message, $BCC);

header("location: Service_Dates.php");

?>

Then funct.php code is:

<?php>
function send_email($from, $to, $subject, $message, $BCC){
	$headers = "From: ".$from."\r\n";
	$headers = "BCC: ".$BCC."\r\n";
	$headers .= "Reply-To: ".$from."\r\n";
	$headers .= "Return-Path: ".$from."\r\n";
	$headers .= "Content-type: text/html\r\n"; 

	if (mail($to,$subject,$message,$headers) ) {
	   echo "email sent";
	} else {
	   echo "email couldn't be sent";
	}
}
?>

I appreciate any help anyone can offer.

If the 'BCC' field is anything like the "To" field, you would probably format it like this: $BCC = "recipient1@somewhere.net, [email]recipient2@somewhereelse.net[/email]"; Using PHP to take the results of the query, you could do something like this to get the BCC recipients into the format I mentioned:

$bcc_email = mysql_fetch_assoc($result);
$index = 1;
$BCC = '';
$email_count = count($bcc_email);

foreach ($bcc_email as $address) {
	if($index < $email_count) 
		{ $BCC .= $address . ","; } 
	else 
		{$BCC .= $address;}
	$index++;
}

Also, concerning the HTML formatting of the message, I found this in the PHP manual: $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; . It looks like you left out a ." on that header line you wrote.

If the 'BCC' field is anything like the "To" field, you would probably format it like this: $BCC = "recipient1@somewhere.net, [email]recipient2@somewhereelse.net[/email]"; Using PHP to take the results of the query, you could do something like this to get the BCC recipients into the format I mentioned:

$bcc_email = mysql_fetch_assoc($result);
$index = 1;
$BCC = '';
$email_count = count($bcc_email);

foreach ($bcc_email as $address) {
	if($index < $email_count) 
		{ $BCC .= $address . ","; } 
	else 
		{$BCC .= $address;}
	$index++;
}

Also, concerning the HTML formatting of the message, I found this in the PHP manual: $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; . It looks like you left out a ." on that header line you wrote.

Oddest thing???

This is my CODE:

// Get all the Emails to include for a BBC bulk email
$query = "select Email FROM Ev_SchVolunteers  WHERE id_Event='$id_Event'";
print_r($query);
$results = mysql_query($query) or die("Error performing query");
//print_r($results);

$bcc_email = mysql_fetch_assoc($results);
$index = 1;
$BCC = '';
$email_count = count($bcc_email);
print_r($bcc_email);
print_r($email_count);

foreach ($bcc_email as $address) {

	if($index < $email_count) 
		{ $BCC .= $address . ","; } 
	else 
		{$BCC .= $address;}
	$index++;
}

print_r($BCC);
exit();

The $sql result reads:

select Email FROM Ev_SchVolunteers WHERE id_Event='1'

I have 4 records that criteria. Wg=hen I use PHP it finds only ONE. When I paste this into SequelPro (mySQL Admin Client) it produces 4 records.

Obviously, if the RESULTS are wrong, I can't get the right count, and can't get the Email Array I am searching for.

Little help...

I wasn't actually trying the code out as I wrote it; it was just an idea of how you might go about it.

Anyway, here's something that's more likely to work.

$index = 1;
$BCC = '';

//count the number of rows in the result set
$email_count = mysql_num_rows($results);

//as long as we haven't reached the end of the result set
while ($row = mysql_fetch_assoc($results)) {

	//if $index is less than the total number of results, concatenate an e-mail address with a comma
	if($index < $email_count) 

		//add an e-mail address with a comma
		{ $BCC .= $row['Email'] . ","; } 

	else 

		//add an e-mail address without the comma if it's the last result
		{ $BCC .= $row['Email']; }

	//increment index for the next round
	$index++;
}

Pretty much all I relied on to give you this were the pages in the PHP manual here and here.

The code you gave is working correctly now as far as OUTPUT. All variables are getting populated correctly, although the mail() call is producing odd behaviors:

1) Even though I declare a $from in the call - my PHP server does notpick it up. It uses $from@PBD5-adweb99.local instead.

2) The BCC address are not being sent.

Is there an option that needs to be enabled on the PHP server on the machine to recognize these calls. It is on a MAC.

Please let me know ASAP what the issue is.

BTW - this is the final code I used, just in case there something there that's off:

<?
$Vol_Name = $_POST['Name'];
$id_Event = $_POST['id_Event'];
$Note = $_POST['Note'];
$sDate = $_POST['sDate'];

	require_once('auth.php');
	include("inc/interface.php");
	include("inc/funct.php");
	ServerConnect();
	DB_Connect();

$sql="INSERT INTO Ev_Notes (id_Event, Name, Note) VALUES ('$id_Event','$Vol_Name','$Note')";
//print_r($sql);
$result = mysql_query($sql) or die(mysql_error());

// Get all the Emails to include for a BBC bulk email
$query = "select Email FROM Ev_SchVolunteers  WHERE id_Event='$id_Event'";
//print_r($query);
$results = mysql_query($query) or die("Error performing query");
//print_r($results);

$index = 1;
$BCC = '';
$email_count = mysql_num_rows($results);


while ($row = mysql_fetch_assoc($results)) {

	if($index < $email_count) 
		{ $BCC .= $row['Email'] . ","; } 
	else 
		{ $BCC .= $row['Email']; }
	$index++;
}

print_r($BCC);


$from = "donw@mac.net";
$to = "donw@mac.net";
$subject = "New Note regarding the Event Date - {$sDate}";


$message = <<<EOF
<html>
	<head>
	</head>
	<body>
This new note is from: $Vol_Name
<br /><br />
{$Note}
<br />
	<br /><br />
Please do not reply directly to this message from your email program, but go<br />
	back click the link below and log-in to the Village of Hope Worship Site:<br /><br />
	
	<strong><a href="http://www.dwdataconcepts.com/VOH/login-form.php">Village of Hope Worship Site</a></strong>
</body>
</html>
EOF;

print_r($from);
print_r($to);
print_r($subject);
print_r($message);
print_r($BCC);

//exit();
send_email($from, $to, $subject, $message, $BCC);

header("location: Service_Dates.php");

?>

funct.php contains this code:

<?php
function send_email($from, $to, $subject, $message, $BCC){
	
// To send HTML mail, the Content-type header must be set
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
	$headers .= 'To: $from' . "\r\n";
	$headers .= 'From: $from' . "\r\n";
	$headers .= 'Bcc: $BCC' . "\r\n";

	if (mail($to,$subject,$message,$headers) ) {
	   echo "email sent";
	} else {
	   echo "email couldn't be sent";
	}
}
?>

Thanks!

This might sound daft but why not loop it until you have sent the mail to everyone?

This might cause a strain on the server CPU though.

Sometimes the line terminators can be problematic (I think this was mentioned in the comments in the PHP manual). Try changing these "\r\n" to "\n" .

By the way, are you using the mail() function locally, or in a hosting environment? If you're trying to test locally, you might have to change your send method to SMTP. Here's an article about that.

Also, it's possible that the mail setup on your server (if in a hosting environment) may entirely block sending to CC and BCC recipients.

Finally, the way you have written these lines (below), the variables will never be read because PHP will not interpolate variables inside of single quotes. You'll need to either concatenate like you did the first time, or change your single quotes to double so that PHP will put the values of the variables where you intended.

$headers .= 'To: $from' . "\r\n";
	$headers .= 'From: $from' . "\r\n";
	$headers .= 'Bcc: $BCC' . "\r\n";

Oddest thing???

This is my CODE:

// Get all the Emails to include for a BBC bulk email
$query = "select Email FROM Ev_SchVolunteers  WHERE id_Event='$id_Event'";
print_r($query);
$results = mysql_query($query) or die("Error performing query");
//print_r($results);

$bcc_email = mysql_fetch_assoc($results);
$index = 1;
$BCC = '';
$email_count = count($bcc_email);
print_r($bcc_email);
print_r($email_count);

foreach ($bcc_email as $address) {

	if($index < $email_count) 
		{ $BCC .= $address . ","; } 
	else 
		{$BCC .= $address;}
	$index++;
}

print_r($BCC);
exit();

The $sql result reads:

select Email FROM Ev_SchVolunteers WHERE id_Event='1'

I have 4 records that criteria. Wg=hen I use PHP it finds only ONE. When I paste this into SequelPro (mySQL Admin Client) it produces 4 records.

Obviously, if the RESULTS are wrong, I can't get the right count, and can't get the Email Array I am searching for.

Little help...

What you were actually doing wrong was calling mysql_fetch_assoc by itself. It only returns 1 result. After it's called then the internal pointer is moved to the next in the result array and its shown (why you have to loop through them).

Thought I would let you know so you don't make that mistake again.

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.