Search code examples
phpbcc

Adding e-mail address in BCC in a PHP code


I'm trying to figure out how to add an e-mail address in BCC. Since I added more "$headers" to add the blinded e-mail address, the entire code doesn't function anymore.

<?php
// put your email address here
$youremail = '[email protected]';

// if the url field is empty
if(isset($_POST['url']) && $_POST['url'] == ''){


// prepare message 
$body = "Nuovo messaggio dal sito web :

Nome:  $_POST[name]
Azienda:  $_POST[company]
Telefono:  $_POST[phone]
Email:  $_POST[email]
Messaggio:  $_POST[message]";

if( $_POST['email'] && !preg_match( "/[\r\n]/", $_POST['email']) ) {
  $headers = "From: $_POST[email]";
} else {
  $headers = "From: $youremail";
}
$headers .= "Bcc: [email protected]\r\n";

mail($youremail, 'Richiesta Informazioni dal Sito Web', $body, $headers );

}
?>

Solution

  • You need to add line breaks to the first line of your headers too:

    if( $_POST['email'] && !preg_match( "/[\r\n]/", $_POST['email']) ) {
      $headers = "From: $_POST[email]\r\n";
    } else {
      $headers = "From: $youremail\r\n";
    }
    $headers .= "Bcc: [email protected]\r\n";
    
    mail($youremail, 'Richiesta Informazioni dal Sito Web', $body, $headers );
    
    }