To remove the sender IP from the Received header for new mail submissions, use the
header_checks key instead of the
header_checks = regexp:/etc/postfix/header_checks_submission
smtp_header_checks option only applies to mail that is sent from Postfix to external servers whereas the
header_checks option applies to incoming mail sent from your client to Postfix.
See also the How Postfix receives mail at http://www.postfix.org/OVERVIEW.html for an overview of the components, mail goes from smtpd -> cleanup -> incoming queue. The
smtpd process receives mail and injects the
Received header with the sender IP address. The
header_checks(5) option is processed by the
cleanup(8) component which sanitizes email headers.
It is not recommended to set such a
header_checks option globally in your main.cf as this would modify the Received header in all emails, even those received from external servers. Instead, you should configure your client to send email through a dedicated submission service on port 587, and configure Postfix to rewrite the headers for these authenticated submissions only.
/etc/postfix/master.cf, add the following
-o lines after the
submission inet n - y - - smtpd
# Require SASL authentication
# Require TLS transport security, do not leak your credentials in plaintext.
# Disallow unauthenticated users from sending mail through this port.
# Use a different cleanup service (see below)
Time to configure the cleanup service for authenticated submissions. I pick the name
ascleanup to keep it short and aligned, but any name works. To do so, duplicate the cleanup service line in the same
master.cf file, but rename the first field and add a new option to select the filter file:
cleanup unix n - y - 0 cleanup
ascleanup unix n - y - 0 cleanup
(Use of the
pcre table requires installing
postfix-pcre on Debian, that will automatically take care of updating the dynamicmaps.cf file. No further changes are needed for this.)
The final piece is the actual filter configuration in
/etc/postfix/header_checks_submission. You could potentially use something like:
/^Received: .*/ IGNORE
which will remove full Received header line, but instead you can also just drop the
from helo.host (reverse.host.name [192.0.2.1]) part while preserving other information:
/^Received: from [^ ]+ \([^ ]+ \[[IPv0-9a-f:.]+\]\)\s+(.* \(Postfix\) with .+)$/ REPLACE Received: $1
If you did change the
mail_name option, do change the
Postfix word to match your configuration. (This pattern is accurate based on the Postfix source code, smtpd/smtpd.c.)
I tested this with postfix 3.4.7-0+deb10u1 on Debian buster.
For another great description with the same approach, see When sending email with Postfix, how can I hide the sender’s IP and username in the Received header?
With the above modification, the following is turned into
Received: by ...:
Received: from debian (unknown [IPv6:fe80::b036:2ff:fe6e:73f4])
by mail.example.nl (Postfix) with ESMTPSA id 1571B910B
for <[email protected]>; Sun, 12 Jan 2020 02:23:15 +0000 (UTC)