I am making a html form on a website and then that form should be forwarded to an email. I am having a hard time with this. Dead line is near and i do not know what to do. Below is the code:

PHP code:

<?
        if ($_POST){
	   if (!isset($_POST["FirstName"])){
			  echo "First name field empty";
			  return false;
	   }else {
			  $FirstName=$_POST["FirstName"];
	   }
	   if(!isset($_POST["LastName"])){
			 echo "Last name field empty";
			 return false;
	   }else {
			  $LastName=$_POST["LastName"];
	   }
	   if(!isset($_POST["DayPhone"])){
			 echo "Day Phone field empty";
			 return false;
	   }else {
			  $DayPhone=$_POST["DayPhone"];
	   }
	   if(!isset($_POST["EveningPhone"])){
			 echo "Evening Phone field empty";
			 return false;
	   }else {
			  $EveningPhone=$_POST["EveningPhone"];
	   }
	   if(!isset($_POST["email"])){
			 echo "Email field empty";
			 return false;
	   }else {
			  $email=$_POST["email"];
	   }
	   if(!isset($_POST["PreferredLocation"])){
			 echo "Preferred Location field empty";
			 return false;
	   }else {
			  $PreferredLocation=$_POST["PreferredLocation"];
	   }
	   if(!isset($_POST["NumberOfBedrooms"])){
			 echo "Number of bedrooms field empty";
			 return false;
	   }else {
			  $NumberOfBedrooms=$_POST["NumberOfBedrooms"];
	   }
	   if(!isset($_POST["NumberOfBathRooms"])){
			 echo "Number of bathrooms field empty";
			 return false;
	   }else {
			  $NumberOfBathRooms=$_POST["NumberOfBathRooms"];
	   }
	   if (!isset($_POST["SpecialFeatures"])){
			$SpecialFeatures="None";
	   }else{
		   $SpecialFeatures=$_POST["SpecialFeatures"];
	   }
	   if (!isset($_POST["additionalCriteria"])){
			$additionalCriteria="None";
	   }else{
		   $additionalCriteria=$_POST["additionalCriteria"];
	   }
	   
	  	$ToEmail = 'some@something.com'; 
		$EmailSubject = 'Buying a Home form'; 
		$mailheader = "From: ".$email."\r\n"; 
		$mailheader .= "Reply-To: ".$email."\r\n"; 
		$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
		$MESSAGE_BODY = "Name: ".$FirstName." ".$LastName."<br>"; 
		$MESSAGE_BODY .= "Daytime Phone: ".$DayPhone."<br>";
		$MESSAGE_BODY .= "Eveningtime Phone: ".$EveningPhone."<br>"; 
		$MESSAGE_BODY .= "Email: ".$email."<br>"; 
		$MESSAGE_BODY .= "Preferred Location: ".$PreferredLocation."<br>"; 
		$MESSAGE_BODY .= "Number Of Bedrooms: ".$NumberOfBedrooms."<br>";
		$MESSAGE_BODY .= "Number Of Bathrooms: ".$NumberOfBathRooms."<br>"; 
		$MESSAGE_BODY .= "Special Features: ".$SpecialFeatures."<br>";
		$MESSAGE_BODY .= "Additioanl Criteria: ".$additionalCriteria."<br>";
		mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
		header("Location: ?sent=true"); 
	 }
    ?>

HTML code contains a simple form and a submit button. Please help my boss is on my bum regarding this. I am a coop student and this is my first coop job. PLEASE HELP! ASAP :sad:

I REALLY APPRECIATE THE REPLIES.

Recommended Answers

All 9 Replies

What is your problem with this code?

It gives me error on mail function. I think the reason being is that its on my local host and not on air. I told my boss to put it up on the server and it still didnt work.

Error on local host:
"Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\360mm\RealestateNEW\Attempt3\buying.php on line 196"

and

<?php
$to = "navie_phull@hotmail.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: navie@360mediamanagement.com";

mail($to,$subject,$txt,$headers);
echo "success";
?>

works but wont send anything to my email even thought it wont show me any error

check out PHP's SMTP configuration and check your server settings. These can be set (as your error message suggests) either hard coded in the PHP.ini file, or on the fly in code by calling ini_set()

Also, check that there is no firewall or anything blocking SMTP on your local machine and/or webserver, and of course that there is an SMTP server available. I am guessing you are in a Windows server environment, so if you are using Exchange you will need to point your PHP config to the exchange server and provide valid credentials for sending mail.

Hope this helps some.
/H

lol you can't send emails directly from localhost, unless you installed an email system already, PHP need a working email system for it to work

It gives me error on mail function. I think the reason being is that its on my local host and not on air. I told my boss to put it up on the server and it still didnt work.

Error on local host:
"Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\360mm\RealestateNEW\Attempt3\buying.php on line 196"

Do you need to send email from localhost?

Do you need to send email from localhost?

Possibly for development and testing purposes - it can be useful, but is not your primary concern I think.

If you have installed something like WAMP on your local machine then it will handle the mail() functions. HOWEVER, this still requires the access through the network relay etc. for the mail to actually arrive at the other end.

I think the code is fine. Someone told me to use sendmail from linux since servers are unix based. I really don;t know what he was talking about to be frank. But yeah he said its much faster than smtp.

No i do not need to send email from the localhost. Its just for testing purposes. Is there anywhere I can test it? Rather than giving it to my boss to test it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.