Last updated on : June 27, 2013.
Every site owner needs a contact form on his/her site to let the users interact with him/her. It is very crucial factor for interaction with the users/customers for every online business site. Not only contact form, many need to have other types of form like inquiry form, complaint form etc. There are many contact form providers too, but they charge a lot for it.
Here, I am sharing how you can create a contact form on your own absolutely free of cost.
You need: an HTML page (users will fill the form fields on this page), a PHP file (it will process the filled fields) and Server space to host these files.
How to do it?
As you have a site, you very probably can create an HTML page (say contact.html) for your site. You just need to have the form fields on this page.
If your server doesn’t support php, you can still do it. You can use free php file hosts like my3gb. Just point the action of the form to the page link.
Firstly we have to create an HTML form with proper action, method and the desired fields.
<form action=”the php file link” method=”post“>
Name : <input type=”text” name=”name” value=””><br/>
Email : <input type=”text” name=”email” value=””><br/>
Subject : <input type=”text” name=”subject” value=””><br/>
Comment : <textarea name=”comment”></textarea><br/>
<button type=”submit” name=”submit” value=”Submit”>
You can customize this form as you want. Just remember to give names to each field, add proper value to the action attribute and keep method=”post” in the form tag.
Now as the user submits the form, the data will be sent to the php file which will process your data and then send to your email address. Let’s see how..
Edit the php file and add the following code to it-
// Collecting the sent information
// You can add such more fields as your requirement is
// Preparing to send the email
// Email address to which mail is to be sent
$my_email = “your email address, like firstname.lastname@example.org”;
// Subject of the email
$email_subject = “Form submitted : ” . $subject;
// The following headers will probably help your email not to go in the Spam box
$headers = ‘From: ‘.$email.”\r\n”;
$headers .= ‘Reply-To: ‘.$email.”\r\n”;
$headers .= “MIME-Version: 1.0\r\n”;
$headers .= “Content-Type: text/html; charset=ISO-8859-1\r\n”;
// Message of the email. You can customize it as you want
$email_message = “A new query is submitted at contact page.<br/>
<b>Form details : </b><br/>
Name : ” . $name . “<br/>
Email : ” . $email . “<br/>
Subject : ” . $subject . “<br/>
Comment : ” . $comment . “<br/>”;
// Sending the email
$mail_status = mail($myemail, $email_subject, $email_message, $headers);
// Checking if the mail is delivered successfully
echo “<h2>Thank You!</h2><h3>We have received your request. We will get back to you within 3 days except holidays.</h3>”;
echo “<h2>Ooops..</h2><h3>Something went wrong. Email could not be sent..</h3>”;
You can add as many fields in the form you want. Just give each field unique name and assign corresponding php variable for each field at the beginning in the process file. Then call these variables in the message of the mail.
Drawbacks of this form:
1. You need a php file hosted on your own server
2. Mail often goes into the Spam or Junk box (I will be sharing about this issue very soon)
3. We are not validating the form by checking whether all the fields are filled or not
4. We are currently not validating the email address of the user (Please refer my next post for last two points)