Setup Mail Gateway/Forwarding using Postfix

I will show you on how to setup a mail forwarding run in Postfix, which is my MX record will be the email gateway and this server will forward all emails to my mail server which run under cPanel.

What we really need is an MTA (mail transfer agent), application which route your email here and there until all the transaction complete and the email reach the destination. Variables as below:

OS: CentOS 5.5 64bit
MTA version: Postfix 2.3.3
Mail gateway IP:
Mail gateway IP:
Destination server (cPanel):
Domain:, and

1. In this case, I already have MX record which pointing to my cPanel server for 3 domains as below:    MX    10
mail             A    MX    10
mail               A    MX    10
mail             A

2. Lets setup and configure MTA and all required applications. We also need to stop sendmail (by default has been enabled by system), remove sendmail from start-up service, disable SElinux and install Postfix using yum:

service sendmail stop
chkconfig sendmail off
setenforce 0
yum install postfix -y

3. We need to do some configuration to tell Postfix what type of MTA it should be. Edit /etc/postfix/ with text editor and change or uncomment following value:

myhostname =
mydomain = localhost
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, $mydomain
mynetworks_style = host

4. Now the important part starts. In the same file (, find relay_domains options and add all domains that need to be forwarded elsewhere (separate between them with whitespace):

relay_domains =

5. Add following line at the end of line of this file. We tell Postfix to forward the email based on map that we will create in /etc/postfix/transport:

transport_maps = hash:/etc/postfix/transport

6. Lets create the mail route map. Edit /etc/postfix/transport using text editor and add following line: smtp:[] smtp:[] smtp:[]

7. Any changes to this file will required us to run following command so Postfix will update the mail route:

postmap /etc/postfix/transport

8. Postfix will learn to port 25 by default. We might need to add alternative port in case if some of your ISP has block port 25. We will open submission port, 587 as well. Make sure you are not blocking these 2 ports for incoming connection in your firewall. Open /etc/postfix/ and uncomment following line:

submission inet n       -       n       -       -       smtpd

9. Configuration and setup has completed. Now we need to restart Postfix so the latest changes affected:

service postfix restart

10. Check whether Postfix is running correctly:

netstat -tulpn | grep master
tcp        0      0        *                   LISTEN      14710/master
tcp        0      0       *                   LISTEN      14710/master

11. Change the MX records to our new mail gateway. The new MX records will look like below:    MX    10
mail             A
mx               A    MX    10
mail               A
mx                 A    MX    10
mail             A
mx               A

12. Once changes have been made, and DNS propagation is completed, we can test this by try to send email and check the full header. In following example, GroupOn has sent one email to jennifer

Received: from [] (
     by with esmtp (Exim 4.69)
     (envelope-from )
     id 1QmQJs-0000nf-C9
     for [email protected]; Thu, 28 Jul 2011 21:08:10 +0800
Received: from ( [])
     by (Postfix) with ESMTP id AE59D13C8064
     for ; Thu, 28 Jul 2011 21:08:21 +0800 (MYT)

Installation complete. Let me know if you have encounter any problem during this setup. To check your Postfix version, you can use following command:

postconf mail_version

If you want to add domain afterwards, repeat step number 4, 6 and 7 and you are good to go. Cheers!

2 thoughts on “Setup Mail Gateway/Forwarding using Postfix

  1. Hello,
    interesting article. Does this work vice versa?

    E.g. sending an e-mail from my internal server -> relay -> external server?


Leave a Reply

Your email address will not be published. Required fields are marked *