So you’re trying to get Postfix setup as a relay using some external email service like Gmail, SiteGround, etc and you keep getting denied error for trying to send as root@hostname. The error looks something like below:
Please check the 550-"MAIL FROM" settings of your mail client and make sure you set the "MAIL 550 FROM" address to an email account which is hosted on the server. (in reply to RCPT TO command))
The solution is quite simple to do in Postfix!
Here’s how to setup the a static FROM EMAIL Address in postfix so all relayers will end up sending from the address you specify in the configs detailed below.
Tis config changes sender addresses from both local originated, and relayed SMTP mail traffic.
First, add the following lines 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_check
We will rewrite the envelope address from email originating from the server itself.
Add the following line to /etc/postfix/sender_canonical_maps:
/.+/ yoursender@address.com
Next, we rewrite the from address in SMTP relayed e-mail.
Add the following line to /etc/postfix/header_check:
/From:.*/ REPLACE From: yoursender@address.com
This is very useful if you’re using a local relay smtp server which is used by all your multifunctionals and several other applications.
If you use an Office 365 SMTP server, any mail with a different sender address than the email from the authenticated user itself will simply be denied. The above config prevents this.