1

I am trying to edit this script to send a Bcc copy to myself:

$to = $your_email;
$from = "Server Xt<dml_submitbot@noemail.com>";
$subject = "User Sent Msg :: $msg";
$HTMLmessage = $message;

emailHTML($to, $from, $subject, $HTMLmessage);

function emailHTML($to, $from, $subject, $HTMLmessage){

   $semi_rand = md5(time());  
   $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  

   $headers = "From: ".$from;      
   $headers .=
   "\nMIME-Version: 1.0\n" .  
   "Content-Type: multipart/mixed;\n" .  
   " boundary=\"{$mime_boundary}\"";  

   $content .=
   "This is a multi-part message in MIME format.\n\n" .  
   "--{$mime_boundary}\n" .  
   "Content-Type:text/html; charset=\"iso-8859-1\"\n" .  
   "Content-Transfer-Encoding: 7bit\n\n" .  
   $HTMLmessage . "\n\n";  

   $ok = @mail($to, $subject, $content, $headers);  

   if(!$ok) {    
   die("Error sending email");  
   }  
}

i have tried to add this $headers .= "Bcc:email@example.com"\n"; but it does not send out the email... How do I go about modofying this script to make it work?

Abraham P
  • 15,029
  • 13
  • 58
  • 126
user3211770
  • 33
  • 1
  • 3
  • 2
    easiest way: ditch it and use PHPMailer or Swiftmailer. both make adding a bcc into a SINGLE line of code, and also provide far better diagnostics if something goes wrong. as well, you should NEVER use the `@` suppression operator. it's the coding equivalent of stuffing your fingers in your ears and going "lalalalala can't hear you" – Marc B Jan 19 '14 at 09:09
  • You should use a more structured way of building your header fields like `$headers = array('…', '…'); $headers = implode("\r\n", $headers);`. Doing so would eliminate the case of missing a line break between `Content-Type` and `BCC`. – Gumbo Jan 19 '14 at 09:13
  • Your multipart message is also invalid as it’s missing the [final boundary delimiter](http://tools.ietf.org/html/rfc2046#page-20). – Gumbo Jan 19 '14 at 09:17

3 Answers3

4

Separate headers by \r\n.

function emailHTML($to, $from, $subject, $HTMLmessage) {

   $semi_rand = md5(time());  
   $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  

   $headers = "From: ".$from . "\r\n";   
   $headers .= "Bcc: email@example.com\r\n";   
   $headers .=
   "MIME-Version: 1.0\r\n" .  
   "Content-Type: multipart/mixed;\r\n" .  
   " boundary=\"{$mime_boundary}\"";  

   $content .=
   "This is a multi-part message in MIME format.\r\n\r\n" .  
   "--{$mime_boundary}\r\n" .  
   "Content-Type:text/html; charset=\"iso-8859-1\"\r\n" .  
   "Content-Transfer-Encoding: 7bit\r\n\r\n" .  
   $HTMLmessage . "\r\n\r\n";  

   $ok = @mail($to, $subject, $content, $headers);  

   if(!$ok) {    
   die("Error sending email");  
   }  
}
vooD
  • 2,881
  • 2
  • 25
  • 34
1

it looks the order of header is important!!!

$from = "Sender Name<sender@stackoverflow.com>";
$to="receiver@stackoverflow.com";
$headers = "From: $from\r\n";
$headers .= "To: $to\r\n";
$headers .= "Return-Path: <".$to.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Bcc:email@gmail.com\r\n";
$headers .= "Content-Type: text/HTML; charset=ISO-8859-1\r\n";
kavehmb
  • 9,822
  • 1
  • 19
  • 22
0

Is $headers .= "Bcc:email@example.com"\n" the exact syntax that you are using?

You should be receiving an error if so as that isn't valid PHP syntax.

Try changing to something like $headers .= 'Bcc:email@example.com' . "\r\n";

Ian Belcher
  • 5,583
  • 2
  • 34
  • 43