Knowledgebase

Creating a simple, safe contact form

If you've ever wanted a feedback or contact form on your website, you may find the following code helpful.

This code is designed to avoid common problems such as Form Post Hijacking, it also hides your email address from SPAM harvesters, and is very easy to configure.

Suggested Use
Simply create a new file called form.php, containing the code below. Be sure to replace the example email address with your own in the section marked "YOUR EMAIL ADDRESS GOES HERE". Upload the file into your web space, and you should now have a working contact form!

To integrate the code into your own existing web design, copy the code between the <body> and </body> tags into your own HTML.

NOW FOR THE CODE

 

<html>
<head>
            <title>Contact Form</title>
</head>
<body>

<?php

$mailTo = "email@exampledomain.co.nz"; // <----- YOUR EMAIL ADDRESS GOES HERE

if ($HTTP_POST_VARS['message']) {
$mailSubject = "Contact Form";
$mailHeaders = "From:" . ereg_replace("\r|\n", " ", stripslashes($HTTP_POST_VARS['name'])) . " <" . ereg_replace("\r|\n", " ", stripslashes($HTTP_POST_VARS['email'])) . ">\r\nX-Mailer: PHP/" . phpversion() . "\r\n";
$mailParams = "-f" . ereg_replace("\r|\n", " ", stripslashes($HTTP_POST_VARS['email']));
$mailBody = ereg_replace("\r|\n", " ", stripslashes($HTTP_POST_VARS['message']));
mail($mailTo,$mailSubject,$mailBody,$mailHeaders,$mailParams);
?>

Thank you for contacting us.<br />

<?php } else { ?>

Contact Form<br /><br />

<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table cellpadding="3" cellspacing="0" border="0">
<tr>
    <td class="content" valign="top">
        Your Name<br />
    </td>
    <td>
        <input class="input" name="name" type="text" size="20" />
    </td>
</tr>
<tr>
    <td class="content" valign="top">
        Your Email<br />
    </td>
    <td>
        <input class="input" name="email" type="text" size="20" />
    </td>
</tr>
<tr>
    <td class="content" valign="top">
        Your Message<br />
    </td>
    <td>
        <textarea class="input" name="message" cols="30" rows="8"></textarea><br />
    </td>
</tr>
    <tr>
        <td colspan="2" align="right">
                <input class="button" type="submit" value="Send Message" />
        </td>
    </tr>
    </table>
</form>

<?php } ?>

</body>
</html>

  • 114 Users Found This Useful
Was this answer helpful?

Related Articles

How do I create a custom php.ini file?

A custom php.ini file can be constructed and placed in your web account. For instance, using a...

The .htaccess file

What is "the .htaccess file"? The .htaccess file is one of the most powerful tools...

How to change the date.timezone value in PHP?

If your PHP scripts do not show the correct time, the reason is that most probably your hosting...

How to connect using SSH (secure shell)

How to connect using SSH: You are going to need an SSH Client. We recommend a program "PuTTY"....

Disabling catch-all email

If you wish to disable the default catch-all email address - i.e. you will only receive email to...