I have a simple contact page set up with a form and I am sending those details via email using the php mail()
function. When I submit the page its die and says:
Notice: Undefined variable: MESSAGE_BODY in line 9
Anybody has any idea or solution please?
<?php
error_reporting(E_ALL);
if ($_POST["email"]<>'') {
$ToEmail = 'saif0909@yahoo.com';
$EmailSubject = 'Get an instant Quote Now';
$mailheader = "From: ".$_POST["email"]."
";
$mailheader .= "Reply-To: ".$_POST["email"]."
";
$mailheader .= "Content-type: text/html; charset=iso-8859-1
";
$MESSAGE_BODY .= "Company Name".$_POST['companyname']."";
$MESSAGE_BODY = "First Name: ".$_POST["firstname"]."";
$MESSAGE_BODY = "Last Name: ".$_POST["lastname"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY = "Phone Number: ".$_POST["phone"]."";
$MESSAGE_BODY = "Delivery City: ".$_POST["city"]."";
$MESSAGE_BODY = "Delivery Date: ".$_POST["date"]."";
$MESSAGE_BODY = "Duration: ".$_POST["duration"]."";
$MESSAGE_BODY .= "Rental Equipment: ".nl2br($_POST["msg"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
Your message was sent
<?php
} else {
?>
<aside>
<form action="./" method="post" class="form-c">
<fieldset>
<legend>Get an Instant Quote Now</legend>
<p>
<label for="companyname">Company Name</label>
<input type="text" id="companyname" name="companyname" required>
</p>
<p>
<label for="firstname">First Name</label>
<input type="text" id="firstname" name="firstname" required>
</p>
<p>
<label for="lastname">Last Name</label>
<input type="text" id="lastname" name="lastname" required>
</p>
<p>
<label for="email">Email Address</label>
<input type="email" id="email" name="email" required>
</p>
<p>
<label for="phone">Phone</label>
<input type="number" id="phone" name="phone" required>
</p>
<p>
<label for="city">Delivery City</label>
<input type="text" id="city" name="city" required>
</p>
<p>
<label for="date">Delivery Date</label>
<input type="text" id="date" name="date" required>
</p>
<p>
<label for="durationa">Duration</label>
<input type="text" id="duration" name="duration" required>
</p>
<p>
<label for="msg">Enter Rental Equipment</label>
<textarea id="msg" required="" name="msg"></textarea>
</p>
<p class="submit"><button type="submit" name="submit">Proceed</button></p>
</fieldset>
</form>
<?php
};
?>
</aside>
Use isset. Fisrt line should be:
if (isset($_POST["email"]) && $_POST["email"] != "") {
This will trigger a short-cirtuit evaluation of && and the second condition will not cause error if the "email" field is not sent.
Please add $MESSAGE_BODY=''
at the top of your php script. Some thing like this.
<?php
error_reporting(E_ALL);
if (isset($_POST["email"])) {
$MESSAGE_BODY=''; //<-- this line
$ToEmail = 'saif0909@yahoo.com';
$EmailSubject = 'Get an instant Quote Now';
$mailheader = "From: ".$_POST["email"]."
";
$mailheader .= "Reply-To: ".$_POST["email"]."
";
$mailheader .= "Content-type: text/html; charset=iso-8859-1
";
$MESSAGE_BODY .= "Company Name".$_POST['companyname']."";
$MESSAGE_BODY = "First Name: ".$_POST["firstname"]."";
$MESSAGE_BODY = "Last Name: ".$_POST["lastname"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY = "Phone Number: ".$_POST["phone"]."";
$MESSAGE_BODY = "Delivery City: ".$_POST["city"]."";
$MESSAGE_BODY = "Delivery Date: ".$_POST["date"]."";
$MESSAGE_BODY = "Duration: ".$_POST["duration"]."";
$MESSAGE_BODY .= "Rental Equipment: ".nl2br($_POST["msg"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
Try this
error_reporting(E_ALL);
if ($_POST["email"]<>'') {
$ToEmail = 'saif0909@yahoo.com';
$EmailSubject = 'Get an instant Quote Now';
$mailheader = "From: ".$_POST["email"]." ";
$mailheader .= "Reply-To: ".$_POST["email"]." ";
$mailheader .= "Content-type: text/html; charset=iso-8859-1 ";
$MESSAGE_BODY = '';
$MESSAGE_BODY .= "Company Name".$_POST['companyname']."";
$MESSAGE_BODY .= "First Name: ".$_POST["firstname"]."";
$MESSAGE_BODY .= "Last Name: ".$_POST["lastname"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Phone Number: ".$_POST["phone"]."";
$MESSAGE_BODY .= "Delivery City: ".$_POST["city"]."";
$MESSAGE_BODY .= "Delivery Date: ".$_POST["date"]."";
$MESSAGE_BODY .= "Duration: ".$_POST["duration"]."";
$MESSAGE_BODY .= "Rental Equipment: ".nl2br($_POST["msg"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
You can just remove the concatenation on the first declaration of MESSAGE_BODY and add concatenation (.=) to all the other lines where text is added to $MESSAGE_BODY. First one with $MESSAGE_BODY should be like : $MESSAGE_BODY = .... and the rest should be like $MESSAGE_BODY .= ...