Simple PHP Contact Form With HTML Email

Setting up contact forms on website can be a bit of a pain, especially if the client wants loads of custom fields, you not only have to set up the form in HTML its self but then write the fields into your sending script. With this script you won’t need to write your sendmail script over and over again. This script will simply loop through the array of posted variables to gather all of your fields, it will then format them nicely in HTML and send the data to an email addres!

Lets start with a simple form:
[sourcecode language=”html”]


And now for send.php
[sourcecode language=”php”]//Where is it being sent?
$destination = “[email protected]”;
//Thats all you need to do!

$message = “

You have recieved a submission through the form on your website:

//Gather posted variables:
foreach($_POST as $keys => $vars){
$message .= “$keys: $vars
$message .= ”


mail($destination,”Form Submission”,$message,”From: $email\n”.
“Content-Type: text/html; charset=\”iso-8859-1\”\n”.
“Content-Transfer-Encoding: 7bit\n”.
“MIME-Version: 1.0\n”);[/sourcecode]

Let me know how you get on!

5 thoughts on “Simple PHP Contact Form With HTML Email”

  1. Hi,

    for the not yet experienced designers can you explain exactly where in a html doc the php script should be placed and how the 2 codes actually work together.

    hope i am making some kind of sense.

    many thanks

  2. Might be a good idea to sanitise user input. We don’t want the script kiddies getting wild! Maybe line 12 should be:

    $message .= “$keys: ” . htmlspecialchars($vars) . “”;

    Also, the email field should be specially sanitised for user-injected headers.

    By the way, love your website design. It’s fantastic!
    Br, Mo

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>