Sending Email over SMTP using relay server is mostly used in production environments. Let’s look for a solution related to it. The client used by myself is Office 365 and the solution worked perfectly.
When we sent out email from any Linux machines using relays over the SMTP we will get emails in end users inbox as babinlonston@relay_servername.com. But it should get in our valid domain name as babinlonston@linuxsysadmins.local as from address.
Edit the main configuration of postfix and append the below configurations.
# vi /etc/postfix/main.cf
Append below configuration
# Forcing the from address when postfix relays over smtp
sender_canonical_classes = envelope_sender, header_sender
sender_canonical_maps = regexp:/etc/postfix/sender_canonical_maps
header_checks = regexp:/etc/postfix/header_checks
- sender_canonical_classes – 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.
- header_checks – The default value will be empty – These are applied to initial message headers/
Put an entry for sender canonical maps for required user. Refer man sender_canonical_maps under canonical (5)
[root@LINUXSYSADMINSSRV1 ~]# cat /etc/postfix/sender_canonical_maps
/.+/ myemailgroup@linuxsysadmins.local
[root@LINUXSYSADMINSSRV1 ~]#
Whenever the specific header come from all sources should be replaced with your required username and domain name. To turn on content filtering with a header_checks or body_checks table pattern. Refer header_checks(5) and body_checks(5) man page.
# cat /etc/postfix/header_checks
/From:.*/ REPLACE From: myemailgroup@linuxsysadmins.local
Finally the mail will send out without bouncing with proper domain name, to verify the same we can confirm by looking into the /var/log/maillog as shown below.
# tail -f /var/log/maillog
Jun 10 10:23:31 LINUXSYSADMINSSRV1 postfix/pickup[13767]: 287E5745: uid=2000 from=myemailgroup@linuxsysadmins.local
Jun 10 10:23:31 LINUXSYSADMINSSRV1 postfix/cleanup[23036]: 287E5745: replace: header From: myemailgroupMonitoring-Appmyemailgroup@linuxsysadmins.local from local; from=myemailgroup@linuxsysadmins.local to=user1@linuxsysadmins.local: From: myemailgroup@linuxsysadmins.local
Jun 10 10:23:31 LINUXSYSADMINSSRV1 postfix/cleanup[23036]: 287E5745: message-id=<5crtf63.w1iWgdfgh53irpAP%myemailgroup@linuxsysadmins.local>
Jun 10 10:23:31 LINUXSYSADMINSSRV1 postfix/qmgr[13768]: 287E5745: from=myemailgroup@linuxsysadmins.local, size=83655, nrcpt=1 (queue active)
Jun 10 10:23:31 LINUXSYSADMINSSRV1 postfix/pickup[13767]: 2F7B217166: uid=2000 from=
Jun 10 10:23:31 LINUXSYSADMINSSRV1 postfix/cleanup[23036]: 2F7B217166: replace: header From: "(Cron Daemon)" from local; from=myemailgroup@linuxsysadmins.local: From: myemailgroup@linuxsysadmins.local
Jun 10 10:23:31 LINUXSYSADMINSSRV1 postfix/cleanup[23036]: 2F7B217166: message-id=<20190610062331.2F7B217166@LINUXSYSADMINSSRV1>
Jun 10 10:23:31 LINUXSYSADMINSSRV1 postfix/qmgr[13768]: 2F7B217166: from=myemailgroup@linuxsysadmins.local, size=920, nrcpt=1 (queue active)
Jun 10 10:23:31 LINUXSYSADMINSSRV1 postfix/local[23040]: 2F7B217166: to=, orig_to=, relay=local, delay=21, delays=21/0.02/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
Jun 10 10:23:31 LINUXSYSADMINSSRV1 postfix/qmgr[13768]: 2F7B217166: removed
Jun 10 10:23:31 LINUXSYSADMINSSRV1 postfix/smtp[23039]: 287E5745: to=user1@linuxsysadmins.local, relay=192.168.1.250[192.168.1.250]:25, delay=0.14, delays=0.06/0.02/0/0.05, dsn=2.6.0, status=sent (250 2.6.0 <5cfdf763.w1iWyO3sinTirpAP%myemailgroup@linuxsysadmins.local> Queued mail for delivery)
Jun 10 10:23:31 LINUXSYSADMINSSRV1 postfix/qmgr[13768]: 287E5745: removed
Make sure, to disable emails for cron jobs, aide, or send STDOUT (1) and STDERR (2) to null with /dev/null 2>&1
Troubleshooting
To verify the Header and mail flow we can use header from anyone of received email.
- Open up a message by double-clicking on it
- Click File tab in the top left of the message
- Choose Properties from the bottom of the list and you’ll see Internet Headers listed at the bottom
Analysing Header of SMTP/Relay
- Fire up any web browser and launch MXtoolbox
- Click on Analyze Headers at top menu
- Paste the copied header and finally click Analyze Header to find the mail flow.
This it, mail successfully sent through relay over SMTP with a fixed FROM address instead of relay host name. Post for future reference and it may help for someone.