I got to spend some time this weekend setting up a mailserver for my personal domain (adamogrady.id.au) and thought I’d jot down some points on this blog. In particular my mail setup uses a Postfix MTA that forward emails from all addresses to a single user, stores messages in the Maildir format and forwards a copy of everything to my Gmail account (which I use for the web interface and which has been my primary address for some time). In particular I’ve done this so I can use different aliases to sign up to various services (facebook@, twitter@, etc) or give out to different people/groups to keep better track of where emails are coming from (and whose selling my details to spammers).
Firstly we should set up our DNS. Create an A record that will be the hostname of your mail server prepended to the domain such as “mail0.example.com”. Next create an MX record for your domain with a high priority (I use 10) that points to the hostname+domain combo you set just prior. Also make sure that there is a PTR record for the IP your server uses that is set to your domain (you’ll need to go to your server hosting provider for this). Probably good to set up an SPF record in your DNS by creating a TXT record for your domain with the data being your SPF setup (I’ve used
v=spf1 a -all for my domain).
To setup Postfix on Ubuntu 14.04, run
sudo apt-get install postfix and select the option that puts you as an Internet Site and put your domain in the next requested field. Go to /etc/postfix/main.cf and make sure
myhostname is set to the hostname+domain of the server,
myorigin can be
/etc/mailname as long as that file is just the domain.
mynetworks should only have localhost entries in it (such as 127.0.0.1) to prevent it being used as an open relay and make sure the bottom three lines are as follows:
home_mailbox = Maildir/ mailbox_command = virtual_alias_maps = hash:/etc/postfix/virtual
This tells Postfix to use Maildir storage system and sets up to allow virtual aliases. Next create the /etc/postfix/virtual file and set the contents to the following:
@[DOMAIN] root [Gmail ADDRESS]
This sets a “catch-all” for your domain and stores messages in root/Maildir as well as forwarding a copy to the specified Gmail address. Run
postmap /etc/postfix/virtual then
postfix reload to load up the alias and restart Postfix.
To give ourselves some reliability we’ll spin up another (smaller) server, preferably in a different location, to use as a backup store-and-forward mail server. We’ll go back to whatever DNS hosting control panel we use and add a new A record for the new server and another MX record (with a lower priority) pointing to the A record. Install Postfix again, choosing the same options as last time than open your /etc/postfix/main.cf and add the following lines:
relay_domains = example.com smtpd_recipient_restrictions = permit_mynetworks,reject_unauth_destination transport_maps = hash:/etc/postfix/transport
It’s also important to make sure the domain specified in
relay_domains is not also found in
mydestination and if it is, remove it from
mydestination. Then open /etc/postfix/transport and add the following
Where “mail0.example.com” is the hostname+domain or IP of your primary mail server. Run
postmap /etc/postfix/transport then
postfix reload on this server and it should be all configured.