Build a Simple PHP Contact Form

Create a script to process your contact form data in just a few steps or sign up to get more advanced features.

PHP Contact form example with custom design and reCaptcha

Process simple PHP contact form data without disruption

Contact forms are the easiest way to let your visitors get in touch with you. They also let you avoid revealing your email address on the web, which can attract spam.

You can build contact forms using various scripting languages, but PHP is one of the most popular. It's very flexible and simplifies connecting to databases, but isn't always easy to learn, especially for beginners. Don't worry, though – we're going to give you a jump-start with a functional script you can download.

To get the simple PHP contact form script for a simple contact form, just click here and save the file, or download the PHP contact form code on the right.

Want to learn even more? Check out our guide to building jQuery Contact forms with Ajax and PHP.

if ($action=="") /** display the contact form */
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
else/* send the submitted data */
if (($name=="")||($email=="")||($message==""))
echo "All fields are required, please fill <a href=\"\">the form</a> again.";  
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("", $subject, $message, $from);
echo "Email sent!";
PHP contact form code

Step by step tutorial

1. Create a PHP file

A PHP contact form has two parts: the form that visitors see and the PHP script that processes the form data and sends it where you want it to go. Each part of the form can be located either in the same file (as you see in the example script) or in two different files. To create a PHP file, you can copy and paste the demo code above or download the example code file we've provided.

2. Copy the PHP script from above

To gather user data, we'll create a simple PHP contact form that asks for a name, email and message (see above). We've highlighted the PHP contact form code above so you can see how it works.

When your visitors open the page with the form (see a live demo), they'll see the form fields from the HTML code. After filling out the form and clicking the "Send email" button, visitors will stay on the same page but see a message confirming the form was processed. Submitting the form sets a hidden parameter called action that prevents the form from being displayed again, so you won't bother visitors with forms they've already filled out. The PHP script also verifies that all the HTML fields are filled in properly. If even one is left empty, it generates an error message.

Create advanced forms with our web form builder

This page shows you how to create a simple form, which is pretty easy. If you want to gather more data with other types of fields, such as single-choice, multiple-choice, text box or dropdown fields, you'll have to add validation and processing for these fields as well.

Assure safety and accuracy

If spammers start to autofill your form with garbage data, you can add CAPTCHA image verification to stop them. If users mistype their email addresses, turn on form validation to make sure that all emails are formatted properly. And if you want your visitors to send you files, you can let them upload files with PHP, too.

Doing all of this on your own would require advanced knowledge of PHP, but the 123FormBuilder free PHP form generator includes these features and many more to make form building a breeze. Give it a try today!

hands coding on a laptop with glasses, crayons and a cup of coffee
Ready to use templates

Select the type of form you need for your business below or browse through our complete web form templates collection.

Contact form

Use this template

Add this contact form template to your website and get messages sent directly to your inbox. Modify this contact form template easily, no programming skills needed!

Use this template

Sales lead form

Use this template

Use the sales lead form to generate leads and prospects for your business. Modify this lead form template with ease and publish it on your website.

Use this template

Support Request Form

Use this template

Simplify the communication between your customers and your support team by sharing this support request form on your website.

Use this template

Sign up, get your own PHP contact form and publish it on the web.