BenSky Blog
Subscribe to my feed

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:

<form action="send.php" method="post">
Name: <input type="text" name="Name" /><br />
Email: <input type="text" name="Email" /><br />
Message: <textarea name="message"></textarea>
</form>

And now for send.php

//Where is it being sent?
$destination = "test@test.com";
//Thats all you need to do!

$message = "<html>
<body style=\"font-family:Arial; font-size:10pt;\">
Hello,<br>
You have recieved a submission through the form on your website:<br><br>
";
//Gather posted variables:
foreach($_POST as $keys => $vars){
$message .= "<b>$keys</b>: $vars<br>";
}
$message .= "
</body>
</html>
";

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");

Let me know how you get on!


1 Subscribe to comments

Leave a Comment