This tutorial describes several important strategies for securing contact forms and scripts that make use of PHP’s mail() functionality. If your script is sending email based on user input, these tips will help to keep things safe and secure.

Don’t reinvent the wheel

Before getting too deep into writing your own contact form script, it may save you some time to grab one of the many scripts that are available around the Web. Then you can simply check that the recommended security tips in this article have been applied to whichever script you feel like using.

Even easier, if you are using WordPress, you can save time and grab a copy of my very secure contact-form plugin, Simple Basic Contact Form, available for 100% FREE at the WP Plugin Directory. Many of the security techniques described in this article are implemented in SBCF to help make it super secure.

That said, let’s check out some essential security tips for email scripts.

Check for bad emails

There a many ways to check that the user’s email address is properly formatted and free from any malicious strings. Doing so helps to ensure that your email script doesn’t end up getting abused by spammers to send out their crappy spams all over the place. Here are two ways of going about checking for bad email input:

This simple function checks the email input variable and returns true or false, depending on whether or not any malicious characters are found in the email string.

Regardless of how you do it, checking for legit email input is critical to security.

Check bad strings

In addition to checking the user’s specified email address, it’s also important to check other input data, such as the email subject and message. Here is an example function that checks for any bad strings:

So you can call this function for each input; it will return true or false depending on whether or not any bad strings are found.

Check for newlines

Newlines included in posted email data (whether intentional or not) are considered malicious and should be removed, or the email should be rejected. Here is an example of a function that checks for any newline shenanigans:

That will make sure that other types of requests such as GET, HEAD, PUT, et al are not accepted.

Hidden/reverse captcha field

Another way to secure your contact forms is to add a hidden field that only bots can see. So when a human submits the form, the hidden captcha field will be empty. But if a bot submits the form, most likely they will complete all fields, including the hidden captcha. That enables you to check the hidden field and reject the POST request if the hidden field is not empty.

To do so, first add a hidden field to the form, something like this:

<input type="text" name="gotcha" class="gotcha" value="">

Then make sure the field is hidden via CSS:

.gotcha { display: none; }

Lastly, in your contact-form processing script, check the value of $_POST['gotcha'] to make sure it is empty. Here is a basic example:

<?php if (!empty($_POST['gotcha'])) die(); ?>

Of course, you will want to customize and integrate according to your own needs/goals.

Use cookies

If you don’t mind requiring cookies, you can begin a session for the form and then check if $_COOKIE['PHPSESSID'] is set. If not, let the visitor know that cookies are required to use the form. Bots generally don’t waste time with cookies, so this technique should help to keep bots and their scummy spams at bay.

Random string captcha

Another technique is to generate a random string when the visitor visits the form. You can save the string as a session variable, and break into several pieces, with each piece displayed in a different color. Then you can ask the user to enter the green characters before clicking the submit button. When the form is submitted, you can check whether or not the user entered the correct information.

This technique will defeat most bots, and is pretty easy for the user (compared to other hard-to-read captcha images). Alternately instead of a random string, you can ask a simple math question. If you know your audience will speak the same language, another strategy is to ask a simple question. Or whatever, you get the idea.

Check for duplicate data

Spambots typically repeat the same data to populate multiple fields. So a good way to check for bots is to loop through the POST variable and check for duplicate data. Here is an example:

Here we are looping through and checking for http:, https:, [url=, and [url], which spambots are known to use when submitting their stupid spams. Feel free to keep a log and update the strings with any new values that you encounter.

Bonus security tips

Here are a few more security techniques to help validate contact forms and keep spam away from your inbox.

You can also extend/apply these techniques to validate phone numbers, email addresses, et al. Another useful validation technique is to check empty values for required fields such as name, address, email, message, subject, etc. In general required fields should never be empty.

Going further

These tips explain some important security tips for any contact form or email script, but of course much more is possible. If you’re thinking about rolling your own contact-form script, I recommend searching around for additional techniques and tips to lock things down as much as possible. These days, you just can’t risk getting your domain/email flagged as “spam” by Google et al, just because some sloppy coding allowed some lowlife scumbag to take advantage of an insecure script.