Contact Forms That Work and Resist Spam
Build a short, accessible contact form whose messages reliably reach you, with layered spam defenses that rarely get in a real visitor's way.
Key takeaways
- Ask only for what you need to reply: usually a way to respond and the message itself.
- Validate on the server every time, because anything done only in the browser can be bypassed.
- Send form notifications from your own domain and put the visitor's address in Reply-To.
- Layer quiet spam defenses such as a hidden trap field, timing checks and rate limits before adding a challenge.
- Save submissions somewhere besides email and test the whole path regularly, since forms often fail silently.
A good contact form asks for very little, tells people what happened after they press the button, delivers every real message to someone who will read it, and filters automated junk without making humans solve puzzles. The visible form is the easy part. Delivery and abuse resistance happen on the server, where visitors never look.
Whether you use a plugin, a hosted form service or your own code, the same checklist applies.
Keep the form short
Each extra field is a reason to give up. A general contact form rarely needs more than a name, an email address and a message. Ask for a phone number only if you will call, and mark it optional. If your team routes requests, one short topic menu is reasonable.
Collecting less also protects you. Data you never gathered can't leak, and privacy laws in many places expect you to collect only what the purpose requires. Don't invite people to type sensitive details such as health information or card numbers into a general message box.
Offer another route as well. A published email address or phone number rescues the visitor whose browser extension, network or disability makes your form unusable.
Build it so everyone can use it
Give every field a visible label linked to the input. Placeholder text is a poor substitute because it disappears once typing starts. Use the right input types, which bring up suitable phone keyboards, and autocomplete values so browsers can fill known details.
<form action="/contact" method="post">
<label for="name">Your name</label>
<input id="name" name="name" autocomplete="name" required maxlength="100">
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>
<label for="message">Message</label>
<textarea id="message" name="message" required maxlength="5000"></textarea>
<button type="submit">Send message</button>
</form> When something is wrong, say which field and how to fix it, next to the field, and keep what the person already typed. Color alone shouldn't carry the error. There is more on wording in error messages that help.
After a successful send, show a clear confirmation and say when to expect a reply. Redirecting to a confirmation page after the submission also stops a page refresh from sending the message twice.
Validate on the server
Browser checks such as required are a convenience. A script can post directly to your form's address and skip them entirely, so your server must check everything again:
- Required fields are present and within length limits.
- The email address has a plausible shape. Don't use an elaborate pattern that rejects unusual but valid addresses.
- Menu values are ones you actually offered.
- Line breaks are rejected in any value that goes into an email header, such as name, address or subject. Otherwise an abuser may be able to add recipients and use your form to send mail.
- Submitted text is escaped before it is shown in a web page or admin screen, and passed to databases through parameterized queries.
Those last two defend against classic flaws explained in SQL injection and cross-site scripting for builders. Also add protection against cross-site request forgery if your framework offers it, especially on forms available to signed-in users. Mail libraries and mature form plugins handle much of this, which is a good reason not to assemble raw email strings by hand.
Make sure the messages arrive
Forms often "work" on screen while notifications vanish into a spam folder or nowhere at all. The most common cause is sending mail that claims to come from the visitor. Your server isn't authorized to send for their domain, so receiving systems may reject it.
Instead:
- Set the From address to one on your own domain, such as
[email protected]. - Put the visitor's address in the Reply-To header, so pressing reply reaches them.
- Send through an authenticated mail service or your provider's SMTP server rather than an unconfigured web server.
- Publish SPF, DKIM and DMARC records for the sending domain.
SPF lists the servers allowed to send for your domain, and the other two records add signatures and a handling policy. If notifications still disappear, work through why emails land in spam.
Be careful with automatic replies that echo the visitor's message back. A spammer can enter someone else's address and have your server deliver their text to a stranger. If you send an acknowledgment, keep it fixed, don't include submitted content and limit how often one address can receive it.
Layer your spam defenses
No single filter catches everything. Several light ones together stop most automated junk.
A trap field, often called a honeypot, is an input hidden from sighted users that simple bots fill in anyway. Hide it from assistive technology too, take it out of the keyboard tab order and set autocomplete="off", so real people and their autofill leave it alone. A name that autofill won't recognize helps.
A signed timestamp token lets the server reject forms submitted within a second or two of loading, or reused many times. Rate limiting slows floods from one source, though remember that many legitimate people can share an address on a mobile or office network.
Content rules can hold messages with many links or repeated text for review. Quarantine suspicious messages instead of deleting them, and look through the quarantine at first to find false positives.
CAPTCHA services are the heavier option. Modern ones often judge behavior in the background, but they add third-party code, privacy considerations and friction for some users. Read why CAPTCHAs keep changing before choosing one, and verify its token on your server, not just in the page. Broader tactics for comments and signups are in fighting comment and form spam.
Store, secure and delete submissions
Email alone is a fragile record. Save each accepted submission in a database or your content system as well, so you can recover it if delivery fails. Restrict who can read the list, serve the form only over HTTPS and decide how long to keep entries. Delete old ones on that schedule.
Say briefly near the button what you will do with the information, with a link to your privacy notice. If you want to add people to a mailing list, ask separately with an unchecked box. Contacting you is not the same as subscribing.
File uploads raise the stakes considerably. If you must accept them, limit type and size, check the actual file content, store files outside the public web folder, generate your own file names and scan them. If you don't truly need uploads, leave them out.
Test it like it will break
Run these checks after launch, after updates and on a calendar reminder:
- Submit a real message from an outside email account and confirm it lands in the inbox.
- Press reply and check it addresses the sender.
- Try an empty field, a very long message, accented characters and emoji.
- Complete the whole form using only the keyboard, then on a phone.
- Check your mail service's logs for bounces and your quarantine for real messages.
A form can stop working for dull reasons: an expired mail password, a changed DNS record, a plugin update or a full database. Visitors rarely tell you, because the way they would tell you is broken. A scheduled test message costs a minute and catches it early.
Choose how to build it
You have three common routes, and each moves the work to a different place.
| Route | What you manage | Watch for |
|---|---|---|
| Form plugin or builder block | Settings, updates, mail delivery | Abandoned plugins, spam settings left off |
| Hosted form service | The embed and the account | Where data is stored, plan limits, added scripts |
| Your own code | Everything on this page | Header handling, validation, upkeep |
A plugin suits most sites on a content system. A hosted service helps static sites that have no server code of their own. Writing your own makes sense when you already maintain an application and can give the form the same care as the rest of it. Whichever you pick, send yourself a hostile-looking but harmless test, such as a message containing angle brackets and quotation marks, and confirm it displays as plain text.
Decide who answers, and how fast
A form is a promise that someone is listening. Send notifications to a shared address or a help desk queue, so holidays and departures don't strand messages. Name one person who checks it, set a realistic reply time and state that time on the confirmation page.
If a topic menu routes requests to different people, include a fallback for anything that doesn't fit. For urgent matters, such as a safety problem or a locked account, tell visitors to use a faster channel instead of the form.
What to do when spam gets through
Look at what the junk has in common before adding obstacles. If it arrives seconds after page load, tighten timing checks. If it posts without ever loading the page, require the token. If it appears to be typed by people, content filtering and moderation will help more than challenges. Change one thing at a time so you can tell what worked and whether genuine messages dropped too.