IntermediateLesson 12 of 305 min read

Contact forms: collecting messages safely

Build a contact form that is safe and reliable: fields, plugins, nonces, sanitising, spam protection and making sure emails arrive, with real checks.

In this lesson you will

  • Plan and build a contact form
  • Understand nonces and sanitising
  • Reduce spam without losing customers
  • Make emails arrive reliably

About 16 minutes

A form is a door to your inbox

Omar adds a contact form to his plumbing site. Within a week he gets thirty messages, and twenty-nine are spam selling things he does not need. One real customer message is lost among them, and a customer waits three days for a reply. Forms are the most important and the most abused part of small sites. This lesson shows how forms work, how to build one with a plugin and how to make it safe and reliable.

The parts of a form

  • Fields: name, email, message. Ask only for what you need; every extra field loses people.
  • Submit button: a clear label such as ‘Send message’.
  • Confirmation: a message or page that says the form was sent.
  • Notification: the email that arrives in your inbox.
  • Storage: optionally keeping entries in WordPress too.

Choosing a form plugin

WordPress core has no contact form block that sends email, so you add a plugin (lesson 8 explains how to judge one). Look for a plugin that is updated often, offers spam protection, keeps entries in the dashboard and is easy to style. Build the form, add it to the Contact page with its block or shortcode, and send a test.

How a safe form behaves, tested for real

Behind every good form is a few checks. WordPress has built-in helpers for each, which good plugins use. A test script ran them on sample input, including a script tag hidden in a message. The mail function was intercepted so nothing was really sent. The output is real:

nonce valid: yes | wrong action: no
is_email('sam@example'): false | is_email('sam@example.com'): true
name cleaned: [Sam Lee]
message cleaned: [Hi! / Need a quote.]
wp_mail returned: true (captured, nothing really sent)
to: owner@example.com | subject: New enquiry from Sam
honeypot idea: field left empty by people = true

Read the results one by one. A nonce is a one-time token that proves the form came from your own page; it passed for the right action and failed for a wrong one. The email address ‘sam@example’ was rejected, while ‘sam@example.com’ passed. The name and message were sanitised: tags were stripped, so the hidden script never reaches your inbox or database. Finally the message was prepared as an email to the owner, with a Reply-To so that clicking Reply goes to the customer.

Fighting spam

Method How it helps
Honeypot field A hidden field humans leave empty; bots fill it
CAPTCHA or similar Challenges bots; keep it light for real people
Rate limits Slows floods of messages
Required fields and checks Rejects obviously false entries
Message filters Block links or banned words

Start with the gentlest method that works, because every extra obstacle also loses genuine customers. Check your spam folder from time to time.

Making sure emails arrive

A common surprise: the form says ‘sent’, but nothing arrives. Many hosts limit the default mail function, and receiving providers distrust messages that appear to come from nowhere. The fix is to send through a proper mail service: install a reputable SMTP plugin, connect it to a real mailbox or mail service, and use a ‘from’ address on your own domain. Then send test messages to Gmail and another provider and look in the spam folder too.

Privacy and trust

  • Say what you do with the data, and link to your privacy policy beside the button.
  • Do not collect more than you need.
  • If you keep entries, delete old ones regularly.
  • If you add a newsletter tick box, leave it unticked by default and follow the rules where you live.

After the message arrives

A form is only half the job; the reply is the other half. Set a promise on the page: ‘We reply within one working day’. Use a short template for common questions, and keep a record of messages that need follow-up. If you get many enquiries, a simple spreadsheet or a customer-management tool helps you avoid losing anyone.

Practice

Build a contact form with name, email and message. Add it to your Contact page. Send three tests: a normal message, one with a missing email and one with a short fake link. Check that each behaves sensibly, and that the real message arrives in two different mail accounts.

Practice answer

The normal message should arrive with a working Reply-To. The one with a missing email should be refused with a clear note. If mail does not arrive, move to an SMTP plugin and a from-address on your domain.

Common mistake

Never testing the form. A form that silently fails costs real customers. Test after every plugin update or hosting change.

A simple form checklist

Check Done?
Form has only needed fields Name, email, message
Privacy link beside the button Yes
Test message arrives In two mailboxes
Spam protection on Honeypot first

Print this list and tick it each time you change the form, the theme or the mail settings. Small checks like this prevent the silent failures that cost the most.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What is a nonce?

  1. A type of image
  2. A one-time token proving the form came from your page
  3. A password
  4. A theme

It passed for the right action and failed for another.

Why sanitise form input?

  1. To make it longer
  2. To change colours
  3. To strip dangerous tags before saving
  4. To add images

The script tag was removed in the real test.

What is a honeypot field?

  1. A hidden field that only bots fill in
  2. A discount code
  3. A menu
  4. A backup

Humans never see it.

What often fixes emails that never arrive?

  1. Using a bigger button
  2. Deleting the page
  3. Sending through an SMTP service from your own domain
  4. Adding tags

Test several mail providers.

What does a Reply-To header do?

  1. Sends your reply to the customer’s address
  2. Blocks spam
  3. Changes the subject
  4. Hides the email

It makes replying simple.

Summary

  • Ask only for what you need.
  • Safe forms check a nonce, validate email and sanitise text.
  • Use light spam protection and an SMTP service.
  • Test after every change.

Next lesson

Next: WooCommerce and your first online shop.

Questions

Can I keep form entries in WordPress?

Many form plugins store entries in the dashboard. Check the plugin's settings and delete old entries regularly.

Is CAPTCHA necessary?

Not always. A honeypot and a few rules stop much spam. Add a CAPTCHA only if spam continues.

Why does my form email go to spam?

Usually because it is sent from a different domain than the one in the From address. Use SMTP and an address on your own domain.

Progress is saved in this browser.