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

03-09-2012, 08: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:


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

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

<input name="email" type="text"> <br> </div>

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

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

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




and the mail.php code:
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:
<h1>Thank You!</h1>
<p>We've received your feedback and will get back soon.</p>



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!

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