Setting Up Simple Mail Forwarding With Postfix + NS1

Carl Levine

Carl Levine September 23, 2016

Chances are, you’ve got a domain name that has been waiting in the wings for your next project but you’re not all-in on spinning up a full-blown email server yet. In this article, we’ll go through some basic steps using a Linux box in the cloud, Postfix and NS1’s DNS to make an MX record.

Step 1: Server

Whether you opt to run the server yourself on your own hardware or spin it up in the cloud, there’s some basic things that need to be done to get this all working. These instructions are written for Debian Linux (Ubuntu 14.04 in this case), so this step may vary if you’re using a different type of distribution.

Install Postfix

$ sudo apt-get install postfix

Postfix, as part of its installation, will ask you what kind of server you’re spinning up. Make sure you select “Internet Site”. Select “OK” and continue to the next screen. 


Enter the domain name that you’ll be receiving mail at; press Enter. 


The package will finish up installation after this step. 

Once the installation is complete, the mail server daemon will automatically start. At any time, the following command can be used to ascertain whether or not the service is running.

$ sudo service postfix status
  * postfix is running

You can also check that postfix is running a server on port 25 with the netstat command.

$ sudo netstat -ltnp | grep 25
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      16534/master    
tcp6       0      0 :::25                   :::*                    LISTEN      16534/master    

Step 2: DNS

Alright, now that the server is humming along, let’s make sure that when we do step 3 that we know what to do with that incoming mail.

  1. Log in to your NS1 account, select the zone where you want to start forwarding mail from.

  2. Create a new record. This will be the location where the mail server’s IP will live.

  3. Create another new record. This time, we’ll leave the host box blank, and select the record type MX for Mail Exchanger. We’ll point this record to the new host we created in the last step, and set the priority to 10.



    Top tip, just like playing golf, the lower the number the better off you are; a lower priority number means that the record will be returned first in priority when queried.

Check Your Work:
Pop over to your terminal screen. We want to make sure this new setup is resolving properly. First, we will dig for the domain and specify that we want to see the MX record.

 $ dig b********************g.com mx 
; <<>> DiG 9.9.5-3ubuntu0.8-Ubuntu <<>> b********************g.com mx
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 11798
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512

;; QUESTION SECTION:
;b********************g.com. IN MX

;; ANSWER SECTION:
b********************g.com. 3599 IN MX 10 mail.b********************g.com.

;; Query time: 42 msec
;; SERVER: 2001:4860:4860::8844#53(2001:4860:4860::8844)
;; WHEN: Mon Sep 19 10:14:11 EDT 2016
;; MSG SIZE  rcvd: 77

Next, we’ll make sure that the name the MX record indicates is pointing to the server. 

$ dig mail.b********************g.com a
; <<>> DiG 9.9.5-3ubuntu0.8-Ubuntu <<>> mail.b********************g.com a
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21632
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512

;; QUESTION SECTION:
;mail.b********************g.com. IN A

;; ANSWER SECTION:
mail.b********************g.com. 3599 IN A 104.236.103.54

;; Query time: 59 msec
;; SERVER: 2001:4860:4860::8844#53(2001:4860:4860::8844)
;; WHEN: Mon Sep 19 10:15:15 EDT 2016
;; MSG SIZE  rcvd: 77

Result! Everything looks good, so let’s log back into the server itself and get Postfix ready to start forwarding mail.

Step 3: Configure Postfix

To get started with this, you’ll need to locate the Postfix configuration directory. 

$ postconf | grep config_directory
config_directory = /etc/postfix

Having found that, we’re ready to configure Postfix. Using the text editor of your choice, open the main.cf file.

$ nano /etc/postfix/main.cf

Scroll to the end of the file, then add the following two lines with your specific information. The virtual_alias_domains variable can be set with multiple domain names, which is handy if the box you’re running is receiving mail for more than one domain – list them separated by a space if applicable.

virtual_alias_domains = b********************g.com
virtual_alias_maps = hash:/etc/postfix/virtual 


Save the file, exit the editor.

Next, let’s set up the address mappings:

$ nano /etc/postfix/virtual

In a basic, 1:1 mapping, the first address listed is the new incoming address, followed by a space, then the destination you want to forward to. For additional forwards, append the file as necessary. Save and close the file.

In order for this to all work, you’ll need to refresh the server’s settings. Run the following commands and you’ll be good to go:

$ postmap /etc/postfix/virtual 

$ sudo /etc/init.d/postfix reload

Finally, ensure that the mappings are set:

$ postconf -n | grep virtual
  virtual_alias_domains = b********************g.com
  virtual_alias_maps = hash:/etc/postfix/virtual

Send A Test Message
Once this is all set, send a message to the new forwarding address and make sure it arrives.


BONUS: Other Postfix Forwarding Tricks

In the /etc/postfix/virtual file, there are other things you can do to enhance your mail forwarding.

Forward Mail to multiple destinations by appending the string with additional addresses:

info@b********************g.com m***********e@gmail.com c*********e@gmail.com

Forward emails to any address for a given domain:

@b********************g.com m***********e@gmail.com

Wrapping Up

Now that you’ve set up a Postfix daemon on a server, configured the DNS, and configured Postfix, you’re now forwarding emails! For more helpful tips and tricks for working with NS1’s Intelligent DNS, check out our Developer Hub!