This question already has an answer here:
i am trying to send mail using php. i have removed the comment from the SMTP port and mail but Apache throws undefined variable $header in line no.5. what is wrong here?
<?php include "head.php";?>
<?php
$from= "smechailes@gmail.com";
$Headers = "";
$header .= "MIME-Version: 1.0
";
$header .= "Content-type: text/html; Charset= iso-859-1
";
$header .= "From: ".$from."
";
$to = "setok321@gmail.com";
$subject = "test-mail";
$message ="<h1>hello</h1>";
$mail = mail($to, $subject, $message, $header);
echo (int)$mail;
?>
<?php include "foot.php";?>
</div>
Replace $Headers = "";
by $header = "";
Explanation:
PHP variables are case sensitive.
You have initialised variable $Headers
and assuming it to be $header
.
And concatenating to $header
, which is undefined.
Either change $Headers
to $header
OR
change
$header
to $Headers
at all places.
You are using the concatenating assignment operator when you use .=
:
$header .= "MIME-Version: 1.0
";
is equivalent to:
$header = $header . "MIME-Version: 1.0
";
Meaning $header is used as a part of the assignment and should exist prior.
use below code, because on line 5 $header is not defined you are concatenate the string
<?php include "head.php";?>
<?php
$from= "smechailes@gmail.com";
//$Headers = "";
$header = "MIME-Version: 1.0
";
$header .= "Content-type: text/html; Charset= iso-859-1
";
$header .= "From: ".$from."
";
$to = "setok321@gmail.com";
$subject = "test-mail";
$message ="<h1>hello</h1>";
$mail = mail($to, $subject, $message, $header);
echo (int)$mail;
?>
<?php include "foot.php";?>