I have some web servers running for productive purpose, and they all have to send out mails. So a problem comes, how can I config mail forwardings for these servers with only internal network to send out mails to the public network?
Luckily it’s easy to implement with the help of either sendmail or postfix, the two most common MTAs in Linux world. If you’re using sendmail as your default mail transfer agent, you can enable forwarding by MAIL_HUB, and all incoming mails would be sent to a centralized hub, then processed there.
Here we show an exmaple based on Postfix.
We use postfix to forward different mails to different smtp servers. To be specific, we forward mails that destinated to our own domains to an internal SMTP server, and relay outbound mails to another server which is able to access external network.
By implementing mail transfer like this, we can also speedup mail transfer.
All we need to do is to adjust postfix’s transport mapping table, it’s /etc/postfix/transport.
We just need to add two new lines in this file, it’s like this:
cards.example.com : example.com :[192.168.19.198]
It directs mails for cards.example.com to local mail server, and mails for example.com to another mail server 192.168.19.198 which also has external IP address. The  around the hostname is intended to disable MX lookups.
After making changes, use this command to make it take effect:
$ postmap /etc/postfix/transport
Then you may need to flush mail queue for the Postfix instance:
$ postfix flush
Some other tips may help you as well:
- To check mail queue list, run: mailq
- To remove all mail from the queue, run: postsuper -d ALL
- To remove all mails in the deferred queue, enter: postsuper -d ALL deferred
- To start/stop postfix instance: postfix start and postfix stop
It’s that simple, but much meaningful as you can define other rules in this file.
For any issues, dont forget to raise a ticket at our mail forum.