DIGITAL4FRONT / CODE BASE

Postfix header_checks Not Matching From:? Why Headers Are Skipped and How to Fix It

Find out why Postfix header_checks matches To: but skips From:, how cleanup-generated headers bypass incoming inspection, and when to use smtp_header_checks instead.

Share

# Postfix header_checks Not Matching From:? Why Headers Are Skipped and How to Fix It

You add a Postfix `header_checks` rule for `From:`, verify it with `postmap`, reload Postfix and send a test email. Nothing happens. Change the rule to `To:` and your custom header appears immediately. Is Postfix ignoring the `From:` header?

**Usually not. The most important distinction is whether `From:` was present in the message when Postfix inspected it, or whether Postfix's `cleanup(8)` service generated that header later.** Postfix does not run normal incoming `header_checks` against headers it adds itself. A separate outbound mechanism, `smtp_header_checks`, can be useful when the requirement is to add a header to messages delivered by SMTP.

This issue appeared in a Stack Overflow question about missing Postfix `From:` header matches. The reported test succeeded with `To:` but not `From:`, and verbose cleanup logs appeared to omit the latter. That is consistent with an automatically generated `From:`, but the question does not show the exact message as it entered Postfix, so the specific root cause remains unconfirmed.

Quick answer: where was the From: header created?

Postfix processes mail in stages. Its incoming `header_checks` are part of the `cleanup(8)` processing that takes place **before a message enters the queue**. As part of that same processing, `cleanup(8)` may supply certain missing headers, including `From:`, `To:`, `Date:` and `Message-ID:`. According to the official `header_checks(5)` manual, headers added by `cleanup(8)` are not inspected by those incoming checks.

There are therefore two different cases:

| What happens | Does incoming `header_checks` see `From:`? | What to do |
| --- | --- | --- |
| The sending application supplies a `From:` header in the original message | Normally yes, assuming the relevant checks are enabled for that mail path | Debug the rule, transport, and configuration |
| The original message has no `From:` and Postfix later generates one | No: the newly generated header is outside the incoming inspection stream | Supply `From:` at submission or use a suitable later-stage mechanism |
| Mail leaves through Postfix's SMTP client | Outbound `smtp_header_checks` can examine outgoing headers, including a `From:` that now exists | Consider `smtp_header_checks` if the change belongs at SMTP delivery time |
| The message is delivered locally rather than through the SMTP client | SMTP-delivery checks do not apply to that path | Choose a mechanism for the actual delivery route |

**Do not infer that `From:` is always excluded.** A genuine incoming `From:` header can be checked. The exception concerns a `From:` header generated by Postfix itself.

Reproduce the issue with a small rule

Suppose `/etc/postfix/add_custom_header` contains:

text
/^From:/ PREPEND X-Customheader: somecustomthings

And `/etc/postfix/main.cf` contains:

ini
header_checks = regexp:/etc/postfix/add_custom_header

This instructs Postfix to insert a single `X-Customheader:` line immediately before a matching header encountered by incoming content inspection. It is not an instruction to add a header whenever a message eventually has a `From:` line.

Check the active configuration and map support:

bash
postconf -h header_checks
postconf -m | grep -x regexp
sudo postfix check

The `regexp` table is a **text file**, not a hashed database. You do not need `postmap /etc/postfix/add_custom_header` to compile it. After editing configuration, use:

bash
sudo postfix reload

To verify a single pattern, run:

bash
printf 'From: sender@example.org\n' | \
  postmap -q - regexp:/etc/postfix/add_custom_header

You should see the lookup result containing `PREPEND X-Customheader: somecustomthings`. This only confirms that the regexp **matches the text you provided to `postmap`**. It does not reproduce Postfix's full mail submission, cleanup, queueing and delivery stages.

For a more realistic table check, Postfix also documents `postmap -hmq -` for header-oriented input:

bash
postmap -hmq - regexp:/etc/postfix/add_custom_header < /tmp/with-from.eml

Even that remains a table test, not proof that an SMTP or local-submission message passed through the same inspection stage.

Test with and without an explicit From: header

To distinguish a missing input header from a broken rule, compare **two otherwise similar test messages**. Replace `recipient@your-test-domain.example` with a mailbox you control.

First create a message that includes `From:`:

bash
cat > /tmp/with-from.eml <<'EOF'
From: Sender <sender@example.org>
To: recipient@your-test-domain.example
Subject: Postfix header check - explicit From

This message includes an explicit From header.
EOF

Then create one that deliberately omits it:

bash
cat > /tmp/without-from.eml <<'EOF'
To: recipient@your-test-domain.example
Subject: Postfix header check - missing From

This message does not include a From header.
EOF

Submit the messages via Postfix's sendmail-compatible interface. Run this **only after replacing the example recipient** and only in a test environment or a controlled mail flow:

bash
/usr/sbin/sendmail -i -t < /tmp/with-from.eml
/usr/sbin/sendmail -i -t < /tmp/without-from.eml

Use your destination mailbox's **raw message / show original** view to inspect the complete delivered headers. If the `From:`-based rule prepends `X-Customheader:` for the first message but not the second, you have demonstrated the important difference: the first input contained a header the incoming checker could match; any `From:` subsequently generated for the second message was not inspected as an incoming line.

Do not assume the second test will behave identically on every installation. Automatic insertion depends on submission context and settings such as `local_header_rewrite_clients` and `always_add_missing_headers`, and malformed or incomplete mail can also be rejected or handled differently. Record what is actually received before drawing conclusions.

Confirm which stage adds missing headers

Check the parameters that affect missing-header generation:

bash
postconf -h local_header_rewrite_clients
postconf -h always_add_missing_headers
postconf -h header_from_format

`local_header_rewrite_clients` controls which clients are treated as local for rewriting; `always_add_missing_headers` broadens missing-header insertion, and `header_from_format` controls the format of a Postfix-generated `From:`. Their presence and values help explain your setup, but none of these settings magically makes a generated header available to earlier `header_checks`.

Postfix documents this behaviour in its `cleanup(8)` manual. A common source of confusion is an application that sets the **envelope sender** but does not explicitly supply an RFC 5322 `From:` message header. These are separate concepts: the envelope sender is used for SMTP routing and bounce handling, while `From:` is a field in the message content.

For example, a local command can specify an envelope sender with `sendmail -f`, yet the original message may still lack a `From:` line. Always inspect the raw message submitted to Postfix rather than assuming what a mail client library generated.

Fix 1: add the custom header while sending outbound SMTP

If your requirement is **a fixed header on mail delivered through Postfix's SMTP client**, the cleanest Postfix-only solution is often to use `smtp_header_checks`. This is a separate inspection stage from incoming `header_checks`; it runs while Postfix sends mail by SMTP.

Create `/etc/postfix/smtp_header_checks`:

text
/^From:/ PREPEND X-Customheader: somecustomthings

Then configure `/etc/postfix/main.cf`:

ini
smtp_header_checks = regexp:/etc/postfix/smtp_header_checks

Check and reload:

bash
postconf -h smtp_header_checks
printf 'From: sender@example.org\n' | \
  postmap -q - regexp:/etc/postfix/smtp_header_checks
sudo postfix check
sudo postfix reload

Now send a message to a **controlled mailbox reached by SMTP** and inspect its complete raw headers. If the outgoing message contains a `From:` header, the SMTP delivery-time rule can prepend the custom header before that line, including when `cleanup(8)` supplied the missing `From:` earlier.

**Scope matters:** `smtp_header_checks` does not cover messages delivered through local or other non-SMTP delivery agents. Nor does this rule retroactively modify messages already queued for local delivery. If you have more than one SMTP transport or explicit `master.cf` overrides, check which configuration applies to the actual path.

Be cautious with production mail: an outbound header modification may interact with downstream signing, filtering, or compliance tooling. Test against your DKIM signing and message-verification setup before enabling a rule globally. A custom `X-` header is not an authentication or anti-spoofing guarantee.

Fix 2: preserve incoming header_checks and target a supplied header

If your mail-producing application is under your control, explicitly supply the `From:` field as part of the original message. Then `header_checks` can match that line during initial cleanup, subject to other filtering settings.

If your only aim is to add the **same static header to every message** during incoming inspection, you might choose another header that you know your application supplies, such as `Subject:`. The original Stack Overflow test succeeded when switching the trigger to `To:`. But that is a conditional workaround, not a universal guarantee: messages can lack optional headers or receive some headers from Postfix later.

text
/^Subject:/ PREPEND X-Customheader: somecustomthings

This rule runs once when a matching `Subject:` line is checked. A message with no original `Subject:` will not trigger it. Avoid attaching important business or security requirements to the presence of a user-controlled header.

If the application owns the message composition, adding the header **in the application** is generally clearer and more reliable than depending on the mail server to infer which messages need it. This is particularly appropriate for application-specific identifiers, campaign metadata, or a custom trace value that belongs to one particular message.

Why switching to mime_header_checks or body_checks does not solve it

Postfix has distinct inspection classes:

  • `header_checks` examines the initial non-MIME message headers presented for incoming cleanup inspection.
  • `mime_header_checks` is for MIME-related headers, such as `Content-Type` and `Content-Disposition`.
  • `nested_header_checks` handles headers in attached email messages.
  • `body_checks` examines body content rather than the primary RFC 5322 `From:` header.
  • `smtp_header_checks` inspects outgoing SMTP headers at delivery time.

A normal `From:` is **not a MIME header or a body line**. Moving an otherwise correct `/^From:/` expression into `mime_header_checks` or `body_checks` does not cause those mechanisms to inspect a header generated outside their input stream. Refer to the inspection classes in `header_checks(5)` before choosing a setting.

The easily missed /i flag in Postfix regexp tables

The original example used:

text
/^From:/i PREPEND X-Customheader: somecustomthings

Postfix `regexp:` maps are **case-insensitive by default**. Its `regexp_table(5)` documentation describes the `i` flag as a *toggle* of that default, not as the familiar “make it case-insensitive” flag from many programming-language regex APIs. With the usual Postfix regexp-map defaults, appending `/i` can therefore make the comparison **case-sensitive**.

For an uncomplicated header-label test, remove the suffix:

text
/^From:/ PREPEND X-Customheader: somecustomthings

Header field names are case-insensitive by email standards. A case-sensitive expression could fail when the original input uses `from:` rather than `From:`. **However, changing `/i` cannot fix a missing input header:** there is nothing for the regexp to match if Postfix only creates the header later. In the reported question, both regexp versions were tested against literal `From:` samples; that does not prove a case-sensitivity problem caused the reported behaviour.

A safe troubleshooting checklist

When a header-check rule looks correct but does not appear in delivered mail, troubleshoot the entire path rather than changing expressions blindly:

1. **Check the exact input:** Capture the raw headers produced by the sending application before handing the message to Postfix. Is `From:` actually present?
2. **Confirm the active rule:** `postconf -h header_checks` or `postconf -h smtp_header_checks` should point to the intended file. Use `postconf -m` to verify `regexp` support.
3. **Check overrides:** Inspect `postconf -P` for service-specific `master.cf` `-o` overrides that may alter inspection parameters, and examine whether a content filter or service is disabling checks.
4. **Test the rule in isolation:** Use `postmap -q` on a literal header and `postmap -hmq -` on a representative message. A successful table lookup is necessary, but not sufficient.
5. **Run `postfix check` and reload:** Use `sudo postfix check` followed by `sudo postfix reload`. Monitor `/var/log/mail.log`, `/var/log/maillog`, or the journal, depending on your distribution.
6. **Use a temporary diagnostic action:** Change the test rule to `/^From:/ WARN saw an input From header` to produce a log warning when that actual line is inspected. Test separately from your `PREPEND` rule, because Postfix stops at the first matching action for a given input line.
7. **Inspect the raw delivered message:** View full headers, not just the mail client's friendly display. Also check whether your test recipient is actually reached via SMTP.
8. **Avoid duplicate additions:** If an application already sets `X-Customheader:`, an unconditional matching rule might add a second one. Design and verify the flow so a message is not marked twice.

For delivery-path details, the official Postfix built-in content inspection guide explains which messages are subject to incoming checks and why internally generated mail can be treated differently.

Common questions

Why does postmap say PREPEND works, but the delivered message has no header?

Because `postmap` only executes a lookup for text you supply. It does not demonstrate that the real message's `From:` line existed when incoming `header_checks` ran. Test the actual submission path and compare a message that does contain `From:` with one that does not.

Does Postfix always ignore the From: header?

No. A genuine incoming `From:` can match a normal `header_checks` rule. Headers generated by `cleanup(8)` itself are excluded from that earlier inspection stage.

Can smtp_header_checks add an X- header based on From:?

Yes, when the message is delivered by the Postfix SMTP client and the outgoing headers contain `From:`. Confirm the receiving mailbox is reached by SMTP, because local delivery takes a different path.

Do I need to run postmap to build a regexp table?

No. `regexp:` maps are text-based. Use `postmap -q` to test them, not a database-generation step intended for compiled map types.

Is the /i flag needed to match From:, from: and FROM:?

Not in the default `regexp:` configuration. Postfix defaults to case-insensitive matching; the `i` suffix toggles that behaviour. Omitting `/i` avoids a surprising case-sensitive rule.

Should I use a custom header for authentication or message trust?

No. A bare custom mail header is not evidence of identity; another sender may include the same field. Authenticate senders using established mail controls and use custom headers for observability or application metadata only.

Conclusion

When a Postfix `/^From:/` rule appears to be ignored, **determine whether the header existed before `cleanup(8)` ran**. If it was supplied by the sender, incoming `header_checks` can usually inspect it. If Postfix generated it later, that generated header is intentionally outside incoming header inspection. Where you need a fixed header on SMTP-delivered messages, test `smtp_header_checks`; where you control the application, supplying the header at the source is often simpler.

The key debugging principle is to test the **stage of processing**, not just the regex. A matching `postmap` result and a delivered message without the custom header are entirely compatible observations.

References

WhatsApp us