• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

mail question in PHP

Below is the simple mail code. I want to exclude $bizid in the $message from the email to user1@xxx.com, but I need to include $bizid for user2@zzz.com and user3@zzz.com in the BCC section. How can I go about it?

<?php

      require_once('includes/connection.php');

      $name = $_POST['fullname'];
      $bizid = $_POST['bizid'];
      $company = $_POST['company'];
      $email = $_POST['email'];
      $mobile = $_POST['mobile'];
      $enquiry = $_POST['about'];

      mysql_select_db($database_DBConn, $DBConn);
      $query_mysql = "SELECT * FROM company WHERE bizid = '$bizid'";

      $mysql = mysql_query($query_mysql , $DBConn) or die(mysql_error());
      $row_mysql = mysql_fetch_assoc($mysql );
      $totalRows_mysql = mysql_num_rows($mysql );
      
      if($row_mysql['email'] == ''){
      $toto = $email;
      }else{
      $toto =      $row_mysql['email'];
      }
      $subject = 'Enquiry - '.$company;
      $message = 'Hi,'."\r\n\r\n".'Welcome:'."\r\n\r\n".'Your Info:'."\r\n".'Business ID: '.$bizid."\r\n".'Company: '.$company."\r\n".'Email: '.$email."\r\n".'Mobile: '.$mobile."\r\n\r\n".'Your enquiry:'."\r\n".$enquiry;
      $headers = 'From: user1@xxx.com' . "\r\n" .
      $headers .= "Reply-To: user1@xxx.com\r\n";
      $headers .= "Return-Path: user1@xxx.com\r\n";
      $headers .= "CC: \r\n";
      $headers .= "BCC: user2@zzz.com, user3@zzz.com\r\n";
    'Reply-To: user1@xxx.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

mysql_free_result($mysql);
//And close the connection
mysql_close($DBConn);


?>
0
rolandmy
Asked:
rolandmy
6 Solutions
 
Peter KwanCommented:
I don't think you can do that in one e-mail. You should separate into two mails, one email to user1@xxx.com (without bcc) and another email bcc to user2@zzz.com and user3@zzz.com.
0
 
xtermCommented:
pkwan is right, when you use blind carbon copy, there is only one message that goes out, and the relay server then clones it to send to the other recipients.

I have modified your code below to where it will send 3 separate emails instead, if that helps.
<?php

      require_once('includes/connection.php'); 

      $name = $_POST['fullname'];
      $bizid = $_POST['bizid'];
      $company = $_POST['company'];
      $email = $_POST['email'];
      $mobile = $_POST['mobile'];
      $enquiry = $_POST['about'];

      mysql_select_db($database_DBConn, $DBConn);
      $query_mysql = "SELECT * FROM company WHERE bizid = '$bizid'";

      $mysql = mysql_query($query_mysql , $DBConn) or die(mysql_error());
      $row_mysql = mysql_fetch_assoc($mysql );
      $totalRows_mysql = mysql_num_rows($mysql );
      
      if($row_mysql['email'] == ''){
      $toto = $email;
      }else{
      $toto =      $row_mysql['email'];
      }
      $subject = 'Enquiry - '.$company;
      $message = 'Hi,'."\r\n\r\n".'Welcome:'."\r\n\r\n".'Your Info:'."\r\n".'Business ID: '.$bizid."\r\n".'Company: '.$company."\r\n".'Email: '.$email."\r\n".'Mobile: '.$mobile."\r\n\r\n".'Your enquiry:'."\r\n".$enquiry;
      $headers = 'From: user1@xxx.com' . "\r\n" .
      $headers .= "Reply-To: user1@xxx.com\r\n";
      $headers .= "Return-Path: user1@xxx.com\r\n";

      // LINES BELOW REMOVED BY XTERM
      //$headers .= "CC: \r\n";
      //headers .= "BCC: user2@zzz.com, user3@zzz.com\r\n";
    'Reply-To: user1@xxx.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

      // ADDED BY XTERM
      $rcpts=array("$toto","user2@zzz.com", "user3@zzz.com");
      for($i=0; $i<sizeof($rcpts); $i++) {
        mail(rcpts[$i], $subject, $message, $headers);
      }

mysql_free_result($mysql);
//And close the connection
mysql_close($DBConn);


?>

Open in new window

0
 
Peter KwanCommented:
One of the implementation for your reference:

<?php

      require_once('includes/connection.php');

      $name = $_POST['fullname'];
      $bizid = $_POST['bizid'];
      $company = $_POST['company'];
      $email = $_POST['email'];
      $mobile = $_POST['mobile'];
      $enquiry = $_POST['about'];

      mysql_select_db($database_DBConn, $DBConn);
      $query_mysql = "SELECT * FROM company WHERE bizid = '$bizid'";

      $mysql = mysql_query($query_mysql , $DBConn) or die(mysql_error());
      $row_mysql = mysql_fetch_assoc($mysql );
      $totalRows_mysql = mysql_num_rows($mysql );
      
      if($row_mysql['email'] == ''){
      $toto = $email;
      }else{
      $toto =      $row_mysql['email'];
      }
      $subject = 'Enquiry - '.$company;
      $headers = 'From: user1@xxx.com' . "\r\n";
      $headers .= "Reply-To: user1@xxx.com\r\n";
      $headers .= "Return-Path: user1@xxx.com\r\n";
      $headers .= "CC: \r\n";

//      $headers .= "BCC: user2@zzz.com, user3@zzz.com\r\n";
    'Reply-To: user1@xxx.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

	  $message = make_mail_content(false);
      mail($toto, $subject, $message, $headers);

      $headers = 'From: user1@xxx.com' . "\r\n";
      $headers .= "Return-Path: user1@xxx.com\r\n";
      $headers .= "CC: \r\n";
      $headers .= "BCC: user2@zzz.com, user3@zzz.com\r\n";

	  $message = make_mail_content(true);
      mail($toto, $subject, $message, $headers);

mysql_free_result($mysql);
//And close the connection
mysql_close($DBConn);

function make_mail_content($bizid_needed) {
    $message = 'Hi,'."\r\n\r\n".'Welcome:'."\r\n\r\n".'Your Info:'."\r\n";
	if ($bizid_needed) {
		$message .= 'Business ID: '.$bizid."\r\n";
	}
	$message .= 'Company: '.$company."\r\n".'Email: '.$email."\r\n".'Mobile: '.$mobile."\r\n\r\n".'Your enquiry:'."\r\n".$enquiry;
	return $message;
}

?>

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
dhaval_neesaCommented:
You can send multiple email address in "TO" and "BCC" also , please try my code
<html>
<head>
<title></title>
<style type="text/css">
<!--
.row1 {
	font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
	font-size:11px;
	font-type:normal;
	padding:2px;
	background-color:#E6E6E6;
}
.row2 {
	font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
	font-size:11px;
	font-type:normal;
	padding:2px;
	background-color: #F2F2F2;
}
-->
</style>
</head>
<body>
<?php

        //check that variables filled
        if(isset($_POST['submit']))
            {

            //request type
            $test = trim($_POST['test']);
            $test2 = trim($_POST['test2']);
            $test3 = trim($_POST['test3']);
                        


        $recipient2=$email;
        $toaddress = 'email_1@123.com,email_2@123.com';


        $subject = 'subject: ';
        $mailcontent = 'test: '.$test."\n"
                       .'test2: '.$test2."\n"
                       .'Wtest3: '.$test3."\n";
                       



        $fromaddress  = "From: 123@123.com,\r\n";
        $fromaddress  .="BCC: 123@123.com,1235@123.com\r\n";
        //prototype is mail(string to, string subject, string message, string additional_headers, string additional_parameters)
        mail($toaddress, $subject, $mailcontent, $fromaddress);


        if(!mail)
            {
                echo'<table width="100%" border="0" cellpadding="5" cellspacing="0">
                    <tr align="center">
                    <td colspan="1" align="left">
                    <img src="mpsgi.gif" border="0"></td>
                    <tr />
                    <td>
                    <font size="6">Email Form</font>
                    </td>
                    <tr />
                    <td>&nbsp;</td>
                    <tr />
                    <td>There was a problem sending your email: </td>
                    </table>';
                exit;
            }
            else
            {
            
            echo'<table width="100%" border="0" cellpadding="5" cellspacing="0">
                    <tr align="center">
                    <td colspan="1" align="left">
                    <img src="mpsgi.gif" border="0"></td>
                    <tr />
                    <td>
                    <font size="6">Change Control Form</font>
                    </td>
                    <tr />
                    <td>&nbsp;</td>
                    <tr />
                    <td>Thank you for email.</td><tr><td><b>Date: </b>'; 
                    echo'</td>';
                    echo'</tr>
                    </table>';
                exit;
            }
            
        }
        ?>
</body>
</html>

Open in new window

0
 
Ray PaseurCommented:
The script will quickly become an open relay for spam.  You might want to learn about these PHP functions before you release this into the wild.
http://php.net/manual/en/function.filter-var.php
http://php.net/manual/en/function.mysql-real-escape-string.php

See also:
http://php.net/manual/en/tutorial.forms.php
0
 
Hugh McCurdyCommented:
I'm going to backup Ray on this one.  Even I could relay spam through a site setup this way.

Here's another link    http://www.w3schools.com/php/php_ref_filter.asp

Please don't give me any points for this post.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now