Jump to content

Help with bcc in automated email


cynlewman

Recommended Posts

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 by requinix
please use [code] tags when posting code
Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

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;

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.