cynlewman Posted January 21, 2016 Share Posted January 21, 2016 (edited) I need help with PHP code that sends automated email receipts to customers. The problem is the customer can see the bcc email address. Can you help me with what code is needed to make the bcc field blind to customers? <?php //change the email parameters as required. //$ccEmail="xxx@xx.com"; $formName="xxxx"; //set the form name as required. $fromEmail="xxxx@xxxx.com"; //set the form email as required. $subject="Your Order Receipt"; //set the subject of the email. $bccEmail="xxxx@xxxx.com,xxxx@xxxx.com"; define("ROOT_PATH","/path/xxxx.com/web/content/"); $fields_string = ""; foreach($_POST as $key=>$value) { if($fields_string != "") { $fields_string .= "&"; } $fields_string .= $key."=".urlencode($value); } $stringval=rtrim($fields_string); include_once("email_format.php"); //uncomment one of the two lines below to toggle test and live mode. $toEmail=$payer_email; //live mode - uncomment to send to buyers email //$toEmail="infoxxx@xxx.com"; // test mode - uncomment to send to infoxxx@xxx.com for testing if($strTemplate!=""){ WriteLogFile($stringval); SendEmail(); //echo $strTemplate; } function WriteLogFile($stringname){ $today = date("F j, Y, g:i a"); $myFile = ROOT_PATH . "website/xxxx/log/log.txt"; if(file_exists($myFile)){ $fp = fopen($myFile, 'a') or die("can't open file"); fwrite($fp,$stringname . "\n" . $today . "\n"); fclose($fp); } return true; } function SendEmail(){ global $formName,$toEmail,$ccEmail,$bccEmail,$strTemplate,$subject,$fromEmail; require_once "Mail.php"; //$from = "xx@xxx.com"; $from = $fromEmail; $to = $toEmail; $body = $strTemplate; $cc=$ccEmail; $bcc=$bccEmail; /*$host = "smtp.emailsrvr.com"; $username = "XXXXX"; $password = "XXXXX"; $host = "smtp.emailsrvr.com"; $username = "xxxxx@xxxxx.com"; $password = "xxxxxx"; */ $host = "smtp.mailgun.org"; $username = "xxxxxx@xxxxx.com"; $password = "xxxxxxxxxx"; if(!empty($cc) && !empty($bcc)){ $headers = array ('From' => $formName . '<' . $from . '>', 'To' => $to, 'Cc' => $cc, 'Bcc' => $bcc, 'Subject' => $subject, 'Content-type'=> 'text/html', 'charset'=> 'iso-8859-1'); } if(empty($cc) && !empty($bcc)){ $headers = array ('From' => $formName . '<' . $from . '>', 'To' => $to, 'Bcc' => $bcc, 'Subject' => $subject, 'Content-type'=> 'text/html', 'charset'=> 'iso-8859-1'); } if(empty($bcc) && !empty($cc)){ $headers = array ('From' => $formName . '<' . $from . '>', 'To' => $to, 'Cc' => $cc, 'Subject' => $subject, 'Content-type'=> 'text/html', 'charset'=> 'iso-8859-1'); } $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => 587, 'auth' => true, 'username' => $username, 'password' => $password)); if(empty($recipients)){ $recipients = $to; if(!empty($cc)){ $recipients.= "," .$cc; } if(!empty($bcc)){ $recipients.= "," .$bcc; } } echo $recipients; //die($recipients); $mail = $smtp->send($recipients, $headers, $body); if (PEAR::isError($mail)) { //echo("<p>" . $mail->getMessage() . "</p>"); WriteLogFile($mail->getMessage()); die($mail->getMessage()); } else { //echo "Email sent through Server SMTP to " . $recipients; WriteLogFile("Email sent through Server SMTP to " . $recipients); return true; } } ?> Edited January 21, 2016 by requinix please use [code] tags when posting code Quote Link to comment https://forums.phpfreaks.com/topic/300607-help-with-bcc-in-automated-email/ Share on other sites More sharing options...
requinix Posted January 21, 2016 Share Posted January 21, 2016 if(empty($recipients)){ $recipients = $to; if(!empty($cc)){ $recipients.= "," .$cc; } if(!empty($bcc)){ $recipients.= "," .$bcc; } }Don't put the BCC list in the list of recipients. In fact, shouldn't the Mail code handle all that itself? Quote Link to comment https://forums.phpfreaks.com/topic/300607-help-with-bcc-in-automated-email/#findComment-1530196 Share on other sites More sharing options...
cynlewman Posted January 21, 2016 Author Share Posted January 21, 2016 So should I delete the following? Sorry for being such a newbie. Someone else wrote the code and now I'm trying to correct it. if(!empty($bcc)){ $recipients.= "," .$bcc; } Quote Link to comment https://forums.phpfreaks.com/topic/300607-help-with-bcc-in-automated-email/#findComment-1530199 Share on other sites More sharing options...
requinix Posted January 22, 2016 Share Posted January 22, 2016 Tentatively, yes. And the CC ones too. Those should both be handled with the $headers earlier. Quote Link to comment https://forums.phpfreaks.com/topic/300607-help-with-bcc-in-automated-email/#findComment-1530205 Share on other sites More sharing options...
cynlewman Posted January 22, 2016 Author Share Posted January 22, 2016 Can you please be more specific? Is this the exact code I should delete? if(empty($recipients)){ $recipients = $to; if(!empty($cc)){ $recipients.= "," .$cc; } if(!empty($bcc)){ $recipients.= "," .$bcc; } } Quote Link to comment https://forums.phpfreaks.com/topic/300607-help-with-bcc-in-automated-email/#findComment-1530207 Share on other sites More sharing options...
requinix Posted January 22, 2016 Share Posted January 22, 2016 No. Because that would also remove the code dealing with the normal To: recipients. See the $to? Make sure $recipients does not have any information about the CC people or the BCC people. Quote Link to comment https://forums.phpfreaks.com/topic/300607-help-with-bcc-in-automated-email/#findComment-1530214 Share on other sites More sharing options...
cynlewman Posted January 22, 2016 Author Share Posted January 22, 2016 So, is this what I should remove? Before Edit if(empty($recipients)){ $recipients = $to; if(!empty($cc)){ $recipients.= "," .$cc; } if(!empty($bcc)){ $recipients.= "," .$bcc; } } After Edit if(empty($recipients)){ $recipients = $to; } Quote Link to comment https://forums.phpfreaks.com/topic/300607-help-with-bcc-in-automated-email/#findComment-1530233 Share on other sites More sharing options...
ginerjm Posted January 22, 2016 Share Posted January 22, 2016 Your call to the mail() function needs 4 things: a to address a subject string a message body string a headers string The to address s/b something like: $to = "you@domain.com"; The subject $subj = "My email subject"; The message body: $body = "This is my message\nHere is the second line of it\nhere is the 3rd."; The headers: $headers = 'From: me@mymail.com' . "\r\n" . 'Reply-To: me@mymail.com' . "\r\n" . 'Bcc: someone@theirdomain.com'; The call is then: mail($to, $subj, $body, $headers); Pretty simple. Quote Link to comment https://forums.phpfreaks.com/topic/300607-help-with-bcc-in-automated-email/#findComment-1530235 Share on other sites More sharing options...
requinix Posted January 22, 2016 Share Posted January 22, 2016 So, is this what I should remove?Yes. Your call to the mail() function needs 4 things:Using some Mail class, not vanilla mail(). Quote Link to comment https://forums.phpfreaks.com/topic/300607-help-with-bcc-in-automated-email/#findComment-1530243 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.