user2673161 user2673161 - 6 months ago 17
PHP Question

Simple php mail header with variable

I cannot get this header to show the variable needed for some strange reason. I know its something very slight. I know that this works:

$headers .= 'Bcc: example@aol.com' . "\r\n";


Can someone please help me with this. FULL CODE:

<?php

if ($_POST){

$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .= "<hr />Sent from your Website at example.com.";


$to = 'person@aol.com';
$from = $email;

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// THE EMAIL IS NOT SENDING THE SENDER OF THE EMAIL
// the from and bcc do not work as needed!
// More headers, put on double quotes
$headers .= 'From:'.$from."\r\n";

$headers .= 'Bcc: '.$from."\r\n";


// send
if (mail($to,$subject,$message,$headers))
{
echo "Message was successfully sent! $from";

} else {
echo "Something went wrong..";
}



} else {

}

?>

Answer

You are missing a dot (period) after your first 'Bcc: '.

it should be

  $headers .= 'Bcc: ' . $from . "\r\n";

UPDATE:

You need to use double quotes when using special characters like \r and \n. In your original example code you used double quotes, but I see now with your full code that you're not actually using them, you're using single quotes.

Single quoted strings in PHP show special characters as literals.

So your lines should be

$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Bcc: ' . $from . "\r\n";
Comments