PDA

View Full Version : Trying to build a simple contact email form


vapidrob
03-09-2012, 07:11 AM
Hi all. I'm trying to build a simple contact form but cant figure out how to get the Email sent to me to show who its from in the "From" when it goes to the inbox. Here is the HTML and PHP code:

HTML:
<html>
<head></head>
<body>

<form action="mail.php" method="POST">
<div>

<p>Name</p>
<input name="name" type="text"> <br> </div>
<div>

<p>E-Mail</p>
<input name="email" type="text"> <br> </div>
<div>

<p>Telephone</p>
<input name="telephone" type="text"> <br> </div>

<p>Comment</p>
<textarea cols="30" name="comment" rows="9"></textarea>
<br> </div>

<div>
<input name="submit" type="submit" value="Send"> </div>

</form>

</body>

</html>

and the mail.php code:
<?php
if(isset($_POST['submit'])) {
$to = "me@me.com";
$subject = "Email from webform";

// data from visitor
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$telephone = filter_var($_POST['telephone'], FILTER_SANITIZE_STRING);
$comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);

$body = " From: $name_field\n\n E-Mail: $email_field\n\n Telephone: $telephone\n\n Message:\n\n $comment";

mail($to, $subject, $body);

// redirect to confirmation
header('Location: confirmation.htm');
} else {
// handle the error somehow
}
?>

and Confirmation Thank you for submitting page code:
<html>
<head></head>
<body>
<h1>&nbsp;</h1>
<h1>Thank You!</h1>
<p>We've received your feedback and will get back soon.</p>

</body>

</html>

What am I missing, because when I send a test email, it sends all the proper form elements, but from the top of the email headers, the "From" who it was, I always get "Nobody@nobody.com

I just want to be able to put the correct "From" in the email header.

What am I missing with this code?

Thank you for your help in advance!

domedia
03-19-2012, 12:57 PM
vapidrob, you're not setting the from field anywhere.