2.3. Configuring Postfix to forward all emails to a mail relay
If you want to forward all email to a mail relay, you can configure Postfix server as a null client. In this configuration Postfix only forwards mail to a different mail server and is not capable of receiving mail.
Prerequisites
- You have the root access.
-
You have the
postfixpackage installed on your server. - You have the IP address or hostname of the relay host to which you want to forward emails.
Procedure
To prevent Postfix from accepting any local email delivery and making it a null client, edit the
/etc/postfix/main.cffile and make the following changes:Configure Postfix to forward all email by setting the
mydestinationparameter equal to an empty value:mydestination =In this configuration the Postfix server is not a destination for any email and acts as a null client.
Specify the mail relay server that receives the email from your null client:
relayhost = [<ip_address_or_hostname>]The relay host is responsible for the mail delivery. Enclose
<ip_address_or_hostname>in square brackets.Configure the Postfix mail server to listen only on the loopback interface for emails to deliver:
inet_interfaces = loopback-onlyIf you want Postfix to rewrite the sender domain of all outgoing emails to the company domain of your relay mail server, set:
myorigin = relay.example.comTo disable the local mail delivery, add the following directive at the end of the configuration file:
local_transport = error: local delivery disabledAdd the
mynetworksparameter so that Postfix forwards email from the local system originating from the 127.0.0.0/8 IPv4 network and the [::1]/128 IPv6 network to the mail relay server:mynetworks = 127.0.0.0/8, [::1]/128
Verify if the Postfix configuration in the
main.cffile is correct:# postfix checkRestart the
postfixservice to apply the changes:# systemctl restart postfix
Verification
Verify that the email communication is forwarded to the mail relay:
# echo "This is a test message" | mail -s <subject> <user@example.com>
Troubleshooting
-
In case of errors, check the
/var/log/maillogfile.