postfix
Rewrite From header
/etc/postfix/main.cf
myorigin = postfix.local
# rewrite header "From"
smtp_generic_maps = hash:/etc/postfix/generic
smtp_header_checks = regexp:/etc/postfix/header_checks
/etc/postfix/generic
@postfix.local postgres@your.domain.org
/etc/postfix/header_checks
/^From:[[:space:]]+(.*)/ REPLACE From: <postgres@your.domain.org>
Mail queue show
postqueue -p
Message info from the queue
postcat -q <mesageid>
Postfix satellite system
A Postfix satellite system is a configuration where the Postfix mail transfer agent (MTA) is installed on a local machine to receive and handle local emails, and then relays them to an external mail server for delivery to their final destination.
In a Postfix satellite system, the local Postfix MTA is configured to forward all outgoing emails to the external mail server for delivery. The external mail server is responsible for handling all incoming and outgoing emails, as well as performing spam filtering, virus scanning, and other security measures.
Mail aliases
/etc/aliases
specifies which account mail sent to an alias should really be delivered to.
When an email is sent to an alias listed in /etc/aliases, Postfix checks the file to determine where the email should be delivered. The destination can be a local system user, an external email address, or another alias.
Modifications to the /etc/aliases file are not complete until the newaliases command is run to build /etc/aliases.db.
Sender address
/etc/postfix/sender_canonical
used by Postfix to rewrite the sender address of outgoing email messages
When an email message is sent from the local system, Postfix checks the sender address against the rules defined in /etc/postfix/sender_canonical
. If a match is found, the sender address is rewritten according to the specified rules.
Mail headers
/etc/postfix/header_checks
used by Postfix to check and modify the headers of incoming and outgoing email messages.
sender_canonical is used specifically for modifying the sender address of outgoing email messages, while header_checks is used to modify any header information in both incoming and outgoing email messages.
Header_checks is a configuration file used by Postfix to check and modify the headers of both incoming and outgoing email messages.
smtp_header_checks
smtp_header_checks is used to modify the headers of outgoing email messages only. It is applied during the SMTP transaction when the message is being sent out.
The main difference between header_checks
and smtp_header_checks
is that header_checks can modify any header information in both incoming and outgoing email messages, while smtp_header_checks is used specifically for modifying the headers of outgoing email messages during the SMTP transaction.
Envelope address and message address
In Postfix, there are two types of email addresses: envelope addresses and message header addresses.
Envelope addresses are used by the mail delivery system to route and deliver email messages. They are not visible to the end user and are only used by the mail transfer agents (MTAs) to deliver the message to the correct recipient. Envelope addresses include the SMTP MAIL FROM and RCPT TO addresses.
Message header addresses, on the other hand, are the visible email addresses that appear in the email header. They include the From, To, Cc, and Bcc fields.
To change these addresses in Postfix, there are two main methods: sender_canonical and header_checks.
-
sender_canonical_classes = envelope_sender, header_sender
- What addresses are subject to sender_canonical_maps address mapping. By default, sender_canonical_maps address mapping is applied to envelope sender addresses, and to header sender addresses. -
sender_canonical_maps
– Address mapping lookup table for envelope and header sender addresses. It can be configured to replace or map specific envelope addresses with another address. -
Header_checks
is used to modify message header addresses.
In summary, envelope addresses are used by the mail delivery system and can be modified using sender_canonical, while message header addresses are visible to the end user and can be modified using header_checks.
https://9to5answer.com/forwarding-email-with-postfix-via-aws-ses https://www.linuxsysadmins.com/forcing-the-from-address-when-postfix-relays-over-smtp/
Name of the host
/etc/mailname
it should your machine name corresponding in /etc/hosts
contains the domain name part of the FROM address.
Postfix replace address
https://serverfault.com/questions/147921/forcing-the-from-address-when-postfix-relays-over-smtp
The optional generic table specifies an address mapping that applies when mail is delivered (sent) from server.
This is the opposite of canonical mapping, which applies when mail is received by server.
Note: both FROM and TO addresses are matched for replacement for any of generic and canonical tables
Replace only email address and not the sender name
If you want to replace only outgoing email address without changing the display name use following in /etc/postfix/main.cf:
sender_canonical_maps = static:no-reply@<FQDN>
Replace both outgoing address and sender name
To replace outgoing (from) address on outgoing email with a desired display name use:
- Create file /etc/postfix/header_checks with following content:
/^From:[[:space:]]+(.*)/ REPLACE From: "Your Name" <email@company.com>
- Configure postfix to use above configuration using:
cd /etc/postfix
postmap header_checks
postconf -e 'smtp_header_checks = regexp:/etc/postfix/header_checks'
service postfix reload
How to change the From header for messages sent by Postfix
- Add the following line in
/etc/postfix/main.cf
. All outgoing emails will have this address in the FROM field, but the name of the sender will not be modified. Replacewith your fully qualified domain name. sender_canonical_maps = static:no-reply@<FQDN>
- To modify the name as well, you need to create a file in
/etc/postfix/header_checks
which contains this line:/^From:[[:space:]]+(.*)/ REPLACE From: "Your Name" <email@company.com>
- Then run the following commands:
cd /etc/postfix postmap header_checks postconf -e 'smtp_header_checks = regexp:/etc/postfix/header_checks' service postfix reload
https://wiki.4psa.com/display/KB/How+to+change+the+From+header+for+messages+sent+by+Postfix
Check if rule applies
postmap -q "X-Original-IP: 192.168.1.1" pcre:/etc/postfix/smtp_header_checks
Will output if this header is listed in any pcre:/etc/postfix/smtp_header_checks
file
postmap -q "Subject: Some subject" pcre:/etc/postfix/header_checks
Will output if Subject
is listed in pcre:/etc/postfix/header_checks
file
Send test mail to root user
apt install bsd-mailx
yum install mailx
echo "hello from `uptime` `who -q`" | mail -s "`hostname`" root
tail /var/log/maillog
tail /var/log/syslog
tail /var/mail.log
Change sender name
- Add the following to
/etc/postfix/main.cf
:sender_canonical_classes = envelope_sender, header_sender sender_canonical_maps = regexp:/etc/postfix/sender_canonical_maps smtp_header_checks = regexp:/etc/postfix/header_checks
- Rewrite the envelope address from email originating from the server itself via this change in :
/etc/postfix/sender_canonical_maps /.+/ newsender@address.com
- Note: you could use
sender_canonical_maps = static:newsender@address.com
inmain.cf
to skip this file
- Note: you could use
- Rewrite the "from" address in SMTP relayed e-mail using this change in :
/etc/postfix/header_checks /^From:.*/ REPLACE From: newsender@address.com
https://serverfault.com/questions/147921/forcing-the-from-address-when-postfix-relays-over-smtp https://www.linuxsysadmins.com/forcing-the-from-address-when-postfix-relays-over-smtp/ https://serverfault.com/questions/811245/how-to-configure-postfix-to-force-from-field-but-leave-reply-to-alone
Delete all mail queue messages in Postfix
postsuper -d ALL
Include subject in Postfix logs
/etc/postfix/main.cf
smtp_header_checks = regexp:/etc/postfix/header_checks
/etc/postfix/header_checks
/^Subject:/ WARN
Find message by subject in Postfix log
grep -i 'subject' /var/log/mail.log
Troubleshoot Postfix Satellite to Exim mail delivery
Environment:
- cron job sends email to local Postfix
- local Postfix relays email to remote Exim
Troubleshoot steps:
- Test email send to root on local machine
apt install bsd-mailx yum install mailx echo "hello from `uptime` `who -q`" | mail -s "testsubject" root
- Test cron job email send to root on local machine
/etc/cron.d/testmail MAILTO=your@email.com */5 * * * * root echo "testcron"
- You could skip
MAILTO
if you want to test default cron configuration
- You could skip
- Check local Postfix log
grep -i 'testcron' /var/log/mail.log
- Find Postfix Message ID
- Find logs for the message ID
grep -i '2D378140116' /var/log/mail.log status=deferred (bounce or trace service failure)
- Check local Postfix queue
postqueue -p | grep -i -A3 2D378140116 postcat -q 2D378140116
- Look up values for
sender
andFrom
sender: name@company.com From: name@company.com (Cron Daemon)
- Look up values for
- Check remote Exim log
grep -m 1 -B13 -A10 -i 'testcron' /var/log/exim4/rejectlog
- Find Exim Message ID
- Find logs for the message ID
grep -i '1qPIw9-00089q-LK' /var/log/exim4/mainlog rejected after DATA: :reject: Envelope from (name@company.com) and header From (name@company.com) are different
- Change local Postfix config
/etc/postfix/main.cf sender_canonical_classes = envelope_sender, header_sender sender_canonical_maps = static:name@company.com smtp_header_checks = regexp:/etc/postfix/header_checks
/etc/postfix/header_checks /^From:.*/ REPLACE From: name@company.com # Add subject to log /^Subject:/ WARN
- Restart local Postfix
systemctl restart postfix
- Repeat previous steps
Postfix extract email address from
Trim message header From to contain email address only
/etc/postfix/main.cf
smtp_header_checks = regexp:/etc/postfix/header_checks
/etc/postfix/header_checks
# RFC for valid message header From:
## From: dev2@bots.company.com
## From: dev2@bots.company.com (Cron Daemon)
## From: Cron Daemon <dev2@bots.company.com>
# Extract email address
/^From:.*\s<?([a-zA-Z0-9_.+\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9.\-]+)>?/ REPLACE From: $1
SMTP headers
By default (and RFC) SMTP headers are US-ASCII. In order to send non-ASCII characters, you need to use Unicode escape sequences.
Headers check
header_checks
apply for incoming and outgoing.
smtp_header_checks
is applied only for outgoing mail (smtp client)
Check string for regexp match
postmap -q "From: server@bots.company.com (Cron Daemon)" regexp:/etc/postfix/header_checks
Header checks rules
Each message header or message body line is compared against a list of patterns. When a match is found the corresponding action is executed, and the matching process is repeated for the next message header or message body line.
Meaning matching the same header twice is possible with header checks. it's not possible to have multiple rules fire for a single header.
Setup
https://www.freesoftwareservers.com/display/FREES/PostFix+-+Ubuntu+-+CentOS