Lorixen.
EN

Email header injection: stop your contact form sending spam

Short answer: A website contact or quote form that emails you the visitor's message can be hijacked to send spam to other people, if the visitor's name, email address or subject is copied into the email's headers without checks. By slipping line breaks into a field, an attacker can add their own headers, such as extra recipients, and use your server and domain to send junk, damaging your email reputation. Never put raw form input into headers: use a maintained mail library or email API, reject line breaks in single-line fields, put the visitor's address in Reply-To rather than From, and rate-limit the form.

How a contact form becomes a spam relay

Email usssages are made of header lines, such as From, To, Subject and Reply-To, followed by the body. The email standard, RFC 5322, defines header fields as lines beginning with a field name, followed by a colon, a field body and terminated by CRLF, the carriage return and line feed pair.

Now picture a simple form script that builds the email like this: subject line "Enquiry from " plus whatever the visitor typed as their name. If the visitor's "name" contains a line break followed by "Bcc: hundreds of addresses", the mail system sees a new header line. Your form now sends their message to everyone on their list, from your server.

This class of weakness is catalogued as CWE-93, Improper Neutralization of CRLF Sequences, also called CRLF injection: software uses carriage returns and line feeds as special separators but does not neutralise them in input.

Why it hurts a small business

  • Your domain and server send spam, so mailbox providers and blocklists start treating your genuine emails, such as quotes and invoices, as suspicious.
  • Your hosting provider may suspend email or the whole account.
  • Phishing in your name becomes possible if attackers can control content sent from your address.

Older, hand-written PHP form scripts on brochure sites are a common source, but any language can have the flaw.

Where the risk sits

Look at every place form input reaches a header:

  • Subject built from a field, for example "Quote request: " plus the service chosen.
  • From or Reply-To set to the visitor's email address.
  • Visitor's name included in a display name, such as "Jane Smith via website".
  • Any custom header, such as a tracking ID.

The message body is less dangerous for header injection, because line breaks there are expected, but it still needs care if you send HTML email.

How to fix it

Use a maintained mail library or a transactional email API. Well-maintained libraries and email service APIs take recipients, subject and reply address as separate structured fields and encode them properly, which removes most of the risk. PHP's basic mail() function, for example, takes extra headers as a string you assemble yourself, and contributors on its documentation page warn that keeping line breaks out of those values is up to you, which is one reason to avoid building headers by hand.

Reject line breaks in single-line fields. Names, email addresses, phone numbers and subjects never legitimately contain carriage returns or line feeds. Reject the submission, or strip them, before using the values anywhere. Validate email addresses with a maintained validation library, as OWASP's Input Validation Cheat Sheet recommends, rather than a home-made pattern.

Fix the From address. Always send from your own address, such as website@yourdomain, and put the visitor's address in Reply-To. That avoids both header tricks and authentication failures, because your domain's SPF, DKIM and DMARC settings cover your own address, not the visitor's.

Send to a fixed recipient. The To address should come from your configuration, never from the form. If the form lets visitors choose a department, map the choice to an address on the server.

Limit sending. Add rate limiting per IP address and a bot check, and set a daily cap on emails from the form. Even if a flaw exists, the damage stays small.

Test your form

Ask your developer to:

  1. Submit the form with a line break and a fake header in the name and subject fields, using a tool that can send raw requests, against a test mailbox.
  2. Confirm the submission is rejected or the header appears as plain text, not as a real header.
  3. Check the From address is your domain and the visitor's address is in Reply-To.
  4. Review the code for any other place input reaches headers.

Watch for warning signs

  • Bounce messages for emails you never sent.
  • A sudden rise in emails sent by your server or email account.
  • Blocklist notifications or your hosting provider warning about outbound spam.
  • Customers saying your emails now land in spam.

If you see these, take the form offline, check logs for unusual submissions, fix the code and contact your email provider.

A worked example

Picture a hypothetical Cork kitchen-fitting company whose ten-year-old website used a hand-written PHP script to email quote requests. The hosting provider suspended outgoing mail after the server sent thousands of messages overnight; the logs showed submissions with line breaks and extra recipients in the name field.

The new developer replaced the script with a transactional email API, sending from the company's own address with the visitor's email in Reply-To and a fixed recipient. Single-line fields now reject line breaks, the form has a bot check and a rate limit, and the daily number of form emails is capped. Email was restored, and the company's quotes stopped landing in customers' spam folders after a few weeks.

FAQ

Do website builders have this problem?

Hosted form tools from major builders generally handle it. Custom code and old plugins are the usual risk.

Is a CAPTCHA enough?

No. It reduces automated abuse, but the injection flaw remains. Fix the code too.

Should we store submissions as well as email them?

Yes, a copy in a database or form service means nothing is lost if email fails.

If your contact form was written years ago and nobody has looked at it since, send us the page address.

Sources

WhatsAppBook a call