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: 28.90.150.2
Mail gateway IP: forwarder.getmail.com
Destination server (cPanel): 28.90.166.73
Domain: getmail.com, yoursetup.net and mymouse.biz

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

getmail.com.    MX    10    mail.getmail.com.
mail             A          28.90.166.173
 
yoursetup.net.    MX    10    mail.yoursetup.net.
mail               A          28.90.166.173
 
mymouse.biz.    MX    10    mail.mymouse.biz.
mail             A          28.90.166.173

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/main.cf with text editor and change or uncomment following value:

myhostname = forwarder.getmail.com
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 (main.cf), find relay_domains options and add all domains that need to be forwarded elsewhere (separate between them with whitespace):

relay_domains = getmail.com yoursetup.net mymouse.biz

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:

getmail.com smtp:[28.90.166.73]
yoursetup.net smtp:[28.90.166.73]
mymouse.biz smtp:[28.90.166.73]

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/master.cf 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 0.0.0.0:25                  0.0.0.0:*                   LISTEN      14710/master
tcp        0      0 0.0.0.0:587                 0.0.0.0:*                   LISTEN      14710/master

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

getmail.com.    MX    10    mx.getmail.com.
mail             A          28.90.166.173
mx               A          28.90.150.2
 
yoursetup.net.    MX    10    mx.yoursetup.net.
mail               A          28.90.166.173
mx                 A          28.90.150.2
 
mymouse.biz.    MX    10    mx.mymouse.biz.
mail             A          28.90.166.173
mx               A          28.90.150.2

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 @yoursetup.net:

Received: from [28.90.150.2] (helo=forwarder.getmail.com)
     by cpanel.getmail.com with esmtp (Exim 4.69)
     (envelope-from )
     id 1QmQJs-0000nf-C9
     for [email protected]; Thu, 28 Jul 2011 21:08:10 +0800
Received: from mta19g.e.groupon.com (mta19g.e.groupon.com [68.232.192.224])
     by forwarder.getmail.com (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?

    Reply

Leave a Reply

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