Create a Simple PHP Contact Form
Creating a contact form in standard HTML isn’t very safe with regards to keeping Spammers away - they’ll see your email address in the HTML and now you’re all setup to receive Spam emails from all over the world!
PHP contact forms are a much safer option because the email address is embedded in the PHP code, and PHP code isn’t visible when a web page is rendered - therefore no one can see the email address you have specified!
The PHP code here is specifically for anyone who wants their contact form to go to two email addresses.
Here is the PHP code for the contact form:
<?php
//Recieve variables
$submit = $_POST[submit];
$name = $_POST[name];
$email_address = $_POST[email_address];
$tel = $_POST[tel];
$enquiry = $_POST[enquiry];
$main_email = "email1@email.com";
$secondary_email = "email2@email.com";
// The Validation for the Form
if(isset($submit)){
if((!strcmp($name, '')) || (!strcmp($email_address, '')) || (!strcmp($enquiry, ''))){
$warning = true;
$message = "Please fill in required fields.";
}
// Process form
if(!isset($warning)){
$email_subject = "SUBJECT OF YOUR EMAIL";
$email_message = "The following form has just been submitted at the YOUR WEBSITE:\r\n\r\n";
$email_message .= "
Name: $name\r\n\r\n
Email: $email_address\r\n\r\n
Tel: $tel\r\n\r\n
Enquiry: $enquiry\r\n\r\n
";
if(mail($main_email, $email_subject, $email_message, "From: YOURMAIL@EMAIL.COM\r\n")){
mail($secondary_email, $email_subject, $email_message, "From: YOURMAIL@EMAIL.COM\r\n");
$sent = true;
}else{
$sent = false;
}
}
}
?>
The HTML Code for the contact form, although very basic (it’s up to you to make it fancy!
) - goes like this:
<?php
if(isset($sent) && $sent == true)
{
echo "Thankyou $name for contacting us. We will get back to you about your enquiry as soon as possible.";
}else{
?>
<?php if(isset($warning)){echo "$message";} ?>
<form method="post" action="contact.php" name="contact">
Name: <input name="name" type="text" value="<?php echo $name; ?>"/>
Email: <input name="email_address" type="text" value="<?php echo $email_address; ?>"/>
Tel: <input name="tel" type="text" value="<?php echo $tel; ?>"/>
Enquiry: <textarea name="enquiry"><?php echo $enquiry; ?></textarea>
<input name="submit" type="submit" value="CONTINUE" />
</form>
<?php } ?>
The form action is the page it goes to once the form has been submitted. I called the page “contact.php”, and the action is “contact.php”, which basically means it reloads the same page but with a “Thank you” message in place of the contact form.
Notice the variables at the top (name, email, tel) etc. they are given the same name as the textfield it responds to. Do not change these as it may cause the contact form to fail.
Other posts in How To...
- Generating Customers via Your Website
- Create a Simple PHP Contact Form
- Centering Your Website Using CSS
- How you should be Link Building
If you enjoyed this post, make sure you subscribe to my RSS feed!
Welcome and thank you for visiting Emz Design!

Advertise on Emz Design for Free - Now!



Dear Webmaster, Where do I put the files on the server? They can see the php file contact.php and contact.html duh? If they can see the contact.php then they also see the mail address.
Best regards,
Danyael
Hi Danyael,
Thanks for your question.
You upload the contact.php to your server as you would any file. There isn’t any need for a contact.html file though - it’s all PHP.
Now, if you view the page in your browser and click view source, you’ll see all the PHP is hidden. Only HTML is rendered in a PHP file. The PHP still works but it doesn’t show when it’s rendered in HTML.
Therefore, gaining access to the email address is impossible because the email address is specified within the PHP, therefore it’s hidden.
hello nice site!
http://theodorejlacour52.blogspot.com
Hello, Thanks for sharing this information. I am learning PHP and found your blog with some informative posts. I have bookmarked it.
Thanks
Thanks for sharing some useful information here. I’m still learning PHP programming and I think I will find some more tips here
Hi Emz, Thank you for the response. The form works great.
Best regards,
Danyael
Hi Emz, I have been working with your form and it’s really great, but now I am in a phase to put some extra things in it. Can you help me out with that?
- I would like to have a select option
- ticker box
- radio button
- some validation
Is that also possible with your form?
Best regards,
Danyael
Apr 7th, 2008 at 9:08 pm
[…] than using HTML contact forms as your email address isn’t visible to world-wide spammers!read more | digg story Share and Enjoy: These icons link to social bookmarking sites where readers can […]