I want to send emails to a list of people who's addresses are extracted from a database.I know the function to be used is 'Bcc:'
but an not able to figure out the correct place where it should be placed in-the code.
It would be good if anyone could come to my rescue.

Thanks in advance

here is the code

<?




If ($action=="mysql"){

	#Grab email addresses from MySQL

	include "./mysql.info.php";

	if (!$sqlhost || !$sqllogin || !$sqlpass || !$sqldb || !$sqlquery){

	print "Please configure mysql.info.php with your MySQL information. All settings in this config file are required.";

	exit;

	}

	$db = mysql_connect($sqlhost, $sqllogin, $sqlpass) or die("Connection to MySQL Failed.");

	mysql_select_db($sqldb, $db) or die("Could not select database $sqldb");

	$result = mysql_query($sqlquery) or die("Query Failed: $sqlquery");

	$numrows = mysql_num_rows($result);



	for($x=0; $x<$numrows; $x++){

	$result_row = mysql_fetch_row($result);

	$oneemail = $result_row[0];

	$emaillist .= $oneemail."\n";

	}

	}



if ($action=="send"){

	$message = urlencode($message);

	$message = ereg_replace("%5C%22", "%22", $message);

	$message = urldecode($message);
	$message = stripslashes($message);
	$subject = stripslashes($subject);

}



?>

<form name="form1" method="post" action="" enctype="multipart/form-data">

  <br>

  <table width="100%" border="0">

    <tr>

      <td width="10%">

        <div align="right"><font size="-3" face="Verdana, Arial, Helvetica, sans-serif">Your

          Email:</font></div>

      </td>

      <td width="18%"><font size="-3" face="Verdana, Arial, Helvetica, sans-serif">

        <input type="text" name="from" value="<? print $from; ?>" size="30">

        </font></td>

      <td width="31%">

        <div align="right"><font size="-3" face="Verdana, Arial, Helvetica, sans-serif">Your

          Name:</font></div>

      </td>

      <td width="41%"><font size="-3" face="Verdana, Arial, Helvetica, sans-serif">

        <input type="text" name="realname" value="<? print $realname; ?>" size="30">

        </font></td>

    </tr>

    <tr>

      <td width="10%">

        <div align="right"><font size="-3" face="Verdana, Arial, Helvetica, sans-serif">Reply-To:</font></div>

      </td>

      <td width="18%"><font size="-3" face="Verdana, Arial, Helvetica, sans-serif">

        <input type="text" name="replyto" value="<? print $replyto; ?>" size="30">

        </font></td>

      <td width="31%">

        <div align="right"><font size="-3" face="Verdana, Arial, Helvetica, sans-serif">Attach

          File:</font></div>

      </td>

      <td width="41%"><font size="-3" face="Verdana, Arial, Helvetica, sans-serif">

        <input type="file" name="file" size="30">

        </font></td>

    </tr>

    <tr>

      <td width="10%">

        <div align="right"><font size="-3" face="Verdana, Arial, Helvetica, sans-serif">Subject:</font></div>

      </td>

      <td colspan="3"><font size="-3" face="Verdana, Arial, Helvetica, sans-serif">

        <input type="text" name="subject" value="<? print $subject; ?>" size="90">

        </font></td>

    </tr>

    <tr valign="top">

      <td colspan="3"><font size="-3" face="Verdana, Arial, Helvetica, sans-serif">

        <textarea name="message" cols="60" rows="10"><? print $message; ?></textarea>

        <br>

        <input type="radio" name="contenttype" value="plain" checked>

        Plain

        <input type="radio" name="contenttype" value="html">

        HTML

        <input type="hidden" name="action" value="send">

        <input type="submit" value="Send eMails">

        </font></td>

      <td width="41%"><font size="-3" face="Verdana, Arial, Helvetica, sans-serif">

        <textarea name="emaillist" cols="30" rows="10">
        <?
    $db = mysql_connect($sqlhost, $sqllogin, $sqlpass) or die("Connection to MySQL Failed.");

	mysql_select_db($sqldb, $db) or die("Could not select database $sqldb"); 
	
	$select=mysql_query("SELECT email FROM test") or die(mysql_error());

	while($row = mysql_fetch_array($select))
  	{
  	echo "".$row['email']."," ;
 
  	}
  
	mysql_query($select,$con);

	mysql_close($con);
	
	
	
	?></textarea>

        </font></td>

    </tr>

  </table>

</form>



<?

if ($action=="send"){



	if (!$from && !$subject && !$message && !$emaillist){

	print "Please complete all fields before sending your message.";

	exit;

	}



	$allemails = split("\n", $emaillist);

	$numemails = count($allemails);



	#Open the file attachment if any, and base64_encode it for email transport

	If ($file_name){

		@copy($file, "./$file_name") or die("The file you are trying to upload couldn't be copied to the server");

		$content = fread(fopen($file,"r"),filesize($file));

		$content = chunk_split(base64_encode($content));

		$uid = strtoupper(md5(uniqid(time())));

		$name = basename($file);

	}



	for($x=0; $x<$numemails; $x++){

		$to = $allemails[$x];

		if ($to){

		$to = ereg_replace(" ", "", $to);

		$message = ereg_replace("&email&", $to, $message);

		$subject = ereg_replace("&email&", $to, $subject);

		print "Sending mail to $to.......";

		flush();

		$header = "From: $realname <$from>\r\nReply-To: $replyto\r\n";

		$header .= "MIME-Version: 1.0\r\n";

		If ($file_name) $header .= "Content-Type: multipart/mixed; boundary=$uid\r\n";

		If ($file_name) $header .= "--$uid\r\n";

		$header .= "Content-Type: text/$contenttype\r\n";

		$header .= "Content-Transfer-Encoding: 8bit\r\n\r\n";

		$header .= "$message\r\n";

		If ($file_name) $header .= "--$uid\r\n";

		If ($file_name) $header .= "Content-Type: $file_type; name=\"$file_name\"\r\n";

		If ($file_name) $header .= "Content-Transfer-Encoding: base64\r\n";

		If ($file_name) $header .= "Content-Disposition: attachment; filename=\"$file_name\"\r\n\r\n";

		If ($file_name) $header .= "$content\r\n";

		If ($file_name) $header .= "--$uid--";

		mail($to, $subject, "", $header);

		print "ok<br>";

		flush();

		}

		}



}


?>

Recommended Answers

All 7 Replies

$header .= "Content-Type: text/$contenttype\r\n";
$header .= "Content-Transfer-Encoding: 8bit\r\n\r\n";

$header .= "$message\r\n";

hey like u concatenated all the header together here.just add one more -

$bcc = "bcc@mail.com" //this is your bcc id
$header = "Bcc: $bcc\r\n";

Thanks for the suggestion
but the email addresses are fetch from database between line 175-184 so the task i need help with is assigning the array a Bcc function such that it willsend Bcc mails to all email addresses

more specifically I need to pass a string of addresses to the variable $bcc from database.So if there is any solution for it.
thanks

Modify the 'for' loop at the 30th line as below -

for($x=0; $x<$numrows; $x++){
		$result_row = mysql_fetch_row($result);
		$oneemail = $result_row[0];
		$oneBcc = $result_row[1];
		$emaillist .= $oneemail."\n";
		$bccList .= $oneBcc."\n";
	}

And modify the $sqlquery also to include the bcc ids also, which you haven't shown it here.
Modify below -

$allemails = split("\n", $emaillist);

	$numemails = count($allemails);

to something like -

$allemails = split("\n", $emaillist);

	$numemails = count($allemails);

	$allbcc = split("\n", $bccList);

	$numBcc = count($allbcc);

and

for($x=0; $x<$numemails; $x++){

		$to = $allemails[$x];
		
		if ($to){

		$to = ereg_replace(" ", "", $to);

		$message = ereg_replace("&email&", $to, $message);

		$subject = ereg_replace("&email&", $to, $subject);

		print "Sending mail to $to.......";

		flush();
....

to something like below -

for($x=0; $x<$numemails; $x++){

		$to = $allemails[$x];
		$bcc = $numBcc[$x];
		
		if ($to){

		$to = ereg_replace(" ", "", $to);
		
		$message = ereg_replace("&email&", $to, $message);

		$subject = ereg_replace("&email&", $to, $subject);

		print "Sending mail to $to.......";

		flush();

		$header = "From: $realname <$from>\r\nReply-To: $replyto\r\n";
		$header .= "Bcc: $bcc\r\n";
		$header .= "MIME-Version: 1.0\r\n";

thanks ,
but still the code is not working ,the bcclist and emaillist are not getting extracted ,and if they do by embedding the code after line 172(not fetched by the code of mysql at top of page) then to the emails sent are not sent as bcc(I tried it and all addresses were visible).

even the for loop in the top part(around line 30)when given a print or echo (to check printing as output of $bccList and $emaillist does not function).

Thanks for your contribution

please post the entire code again, while retaining echos after changes suggested by me in the code.
At least there has to be echo of what you said.
Also specify how is your database structure ?
you have different columns of 'To field' email ids and the 'bcc field' email ids in the database or else the same column contains the email ids i.e lets say there are 10 email ids in the table , all 10 you using for the 'To' and (say) last 5 again using for the bcc field.
Is that the case?

thanks for all the suggestions the problem is now solved .

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.