You are here: Home » All Posts » Contact Forms » Free, simple, PHP based email contact form

Free, simple, PHP based email contact form

in Contact Forms

This page presents the sample code for a simple 'contact us' form. The form collects a few pieces of information ( email, name and a message ) from your visitor and emails it to you.

Note: You can make contact forms quickly with Simfatic Forms. Simfatic Forms helps you to make complete, feature rich forms and get it online quickly. Read more here.

Why to have a contact form?

"Contact us" forms are essential for almost any website. The 'contact us' form provides an easy interface through which your visitors can communicate to you. Your visitors can quickly submit their views, opinions and suggestions about your website , product or service.

The HTML contact form code

We will create a simple contact form with 3 fields: name, email address and a message field. Making the contact form simple (any form – for that matter) gets you more submissions. The more the number of fields, the more reluctant your visitors will be to submit the form.

The HTML code of the form is given below:

<form method="post" name="contact_form"
action="contact-form-handler.php">
    Your Name:
    <input type="text" name="name">
    Email Address:
    <input type="text" name="email">
    Message:
    <textarea name="message"></textarea>
    <input type="submit" value="Submit">
</form>

In the contact form download, the form code is in the email-contact-form.html page. To embed the form in a web page, just copy and paste the HTML form code to the web page.

Validating the form submission

Form validations are essential for any web form. For this simple contact form, we will make all the fields mandatory and will make sure that the email field is in the format: name@domain.tld.

It is better to do validations both on the client side and on the server side. Client side validation provides a quick feedback to your visitor. However, the client side validation can just be bypassed by disabling JavaScript in the browser. Therefore, we need to validate on the server side as well.

For client side validation, we will use the Free JavaScript Form Validation Script. The script is very simple to use and has almost all validation types built-in.

Here is the client side form validation code:

<script language="JavaScript">
var frmvalidator  = new Validator("contactform");
frmvalidator.addValidation("name","req","Please provide your name");
frmvalidator.addValidation("email","req","Please provide your email");
frmvalidator.addValidation("email","email",
  "Please enter a valid email address");
</script>

Server-side processing

Once the contact form is submitted, the form submission data is sent to the script mentioned in the action attribute of the form (contact-form-handler.php in our form). The script then will collect the form submission data, validate it and send the email.

The first step is to validate the data. Ensure that the mandatory fields are filled-in, and that the email is in proper format.

The server-side code is given below:

$errors = '';
$myemail = 'yourname@website.com';//<-----Put Your email address here.
if(empty($_POST['name'])  ||
   empty($_POST['email']) ||
   empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

Emailing the form data using PHP

We will now compose and send the email.

if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}

We first check whether the validations succeeded. If there were errors, the email is not sent. The PHP mail function is used to send the email. After sending the email, the visitor is redirected to the 'thank you' page.

Download the code for the contact form

Click here to download php-email-contact-form.zip

The download contains the code for the HTML form, the validations and the PHP form handler.

More Contact forms:

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Related posts:

  1. How to create PHP based email form with file attachment

{ 48 comments }

Vincent March 27, 2011 at 9:53 pm

found i had to include a \n for the Reply-To to work correctly…
 
$headers = "From: $myemail\n";

Prasanth May 25, 2011 at 8:04 am

Thanks for finding this. corrected now.

B Uscon April 2, 2011 at 10:52 am

Very nice and easy form. How do I add a phone number field? I tried but couldn’t get it to work. Any help will be greatly appreciated.
Thanks

Prasanth April 2, 2011 at 2:28 pm

Please see the sample forms with phone number field.

adrian May 5, 2011 at 2:31 pm

is there something else i have to change to get this to send the email to my hotmail account?

Purushartha May 23, 2011 at 10:14 am

Hello. This is a very helpfull information. Except that the email goes to my junk folder. How do we correct this?
 
Thank you.

Beth June 5, 2011 at 6:05 pm

Why my email is not getting send to the specified email address….what should i do to correct it?
Plz reply ASAP

abhishek June 23, 2011 at 11:50 am

Why my email is not getting send to the specified email address….what should i do to correct it?

Paul June 29, 2011 at 12:00 am

Hi there, thank you so much for your help, anyway I had a quick question.
I uploaded and inserted your code into a website, it works perfectly… but I have a question which looking through the code I cannot find an answer to.
 
The “message” box is somewhat small, and I know it can be dragged to make larger, but how do I make the box larger automatically?  Some people don’t realize you can “make” the box bigger by dragging.  I want to have a message box lets say the double width, and at least 10 line height.
 
Where in the code do I change this setting?
 
Thanks
 
Paul
 

Prasanth June 29, 2011 at 4:59 am

You can add rows and cols attributes to the message textarea

Also see some of the new contact forms

Richard June 29, 2011 at 2:32 am

Hi, great form!

One question though, I noticed you use have set the email to show as coming from the recipient address I am guessing so that it does not go to junk mail in hotmail once you have set hotmail to not put that address in spam.
My question is, is there any other way to stop it from going to spam box in hotmail with having each email showing as coming from the email put into the email: input filed?

halfnets July 2, 2011 at 10:41 am

Hmm i don’t get any email!!! WHY? I change the “contact-form-handler.php” email to my but when i try if work it don’t i enter other mail and name but nothing any ideas????

Stigern July 6, 2011 at 7:51 pm

Can’t get this to work either, updated to my gmail adress where it says change adress.
 
It shows the thanks page.

Billy July 7, 2011 at 4:41 am

Hi, thanks for sharing this. It works great for me~ :)
Sorry that Im new to this. One question here. How am I going to give style (CSS) to the content of the email that I received? In my case, I received email like this:
You have received a new message.  Here are the details:
Name: billy
Email: abc@abc.com
Message
testing
But all the fonts are show in default form. And also when I click reply, my signature that Im using is also set back to the default font. This happen only particular for this mail I received. How am I going to set the style for received content and reply content? Hope one can provide solutions here. Thanks in advance~ :)

Tim July 18, 2011 at 6:17 pm

Thank you for the above code. Looks really good. My question is, how do i add smtp settings to the code?

jason July 22, 2011 at 11:02 am

I am also having an issue. I changed my email address in the .php source as requested, and uploaded the files successfully. I am not however, getting emails when the form is filled – any thoughts?

Prasanth July 25, 2011 at 5:29 am
Rachel July 23, 2011 at 8:02 am

Anybody figure out how to send this to a gmail account?

Darryl Young July 23, 2011 at 2:30 pm

Hey,
Thanks for the useful code, it’s much appreciated. I managed to get it working without problems straight away but I was wondering if it would be possible to display a “Thank you” on the same page that the contact form is located as opposed to redirecting to a separate thank-you.html page. I have a good knowledge of HTML/CSS so I managed to get it all styled and working great but I don’t know enough about PHP to play with the handler.php file. Any help would be greatly appreciated!

Greg August 18, 2011 at 8:34 pm

Hi Darryl, I think I may have posted the same problem that I am having. Let me know if you get a response to this issue. I would too, like to have a new window open up, for the thank you note but not familiar enough with PHP. Hope we can both learn the answer to this.

Darryl Young July 23, 2011 at 2:32 pm

…I’d just like to add (to my previous post) that I understand I could just put the contact form in an iframe on the home page of a website and have that iframe display the thank-you page. I don’t want to use iframes really so if there’s a way to just display a simple string of text like “Thank you” in place of the form that would be great!

Kalon July 27, 2011 at 3:14 pm

I placed this in the companies website and everytime I click submit it asks for login name and password.

Anyone have a clue what that is all about?

Greg August 18, 2011 at 8:32 pm

Kalon, you are probably trying to use the sample that allows you to use password and usernames. Do you need this from people submitting the form? Maybe try the one version that just has the persons name, email address, and comment field. Hope this helps.

JB August 10, 2011 at 3:13 am

i am getting a msg like this. what should i do?
Warning: Cannot modify header information – headers already sent by (output started at /home/content/76/8144676/html/email/contact-form-handler.php:1) in /home/content/76/8144676/html/email/contact-form-handler.php on line 34

Greg August 18, 2011 at 8:30 pm

Is there a way to have a PHP submitted form to NOT leave the present page of a website on where the viewer clicked on the submit button? I would like for a submitted form to open up a new web browser window, and then have the thank you note. I haven’t seen this yet on samples of PHP forms and wondering if this is not possible to do. Any advise would be appreciated!

Morten Beta August 22, 2011 at 5:36 pm

I am having a problem with this browser-wise that is.
Works like a charm in Safari but not in Firefox (on a Mac). Any ideas as to why that is? Thank you. I have removed the message field I only need to collect name and email for my purpose.
Thanks for the code though.

Morten Beta August 22, 2011 at 6:13 pm

I got it. Sorry for the inconvenience!

Comments on this entry are closed.

Previous post:

Next post: