Running a linux mail server (Ubuntu)

02 March 2016 Author: Erik Lievaart While looking for assignments I came to the realisation that I don't have an email address that is representational. In the past I have opened dedicated email address for such new tasks, but then you end up with yet another email address. Wouldn't it be nice if we could open an email adress that is linked to our domain name and forwards to an existing email address?

Since my domain name is tied to a linux droplet hosted on digital ocean, running a mail server on the droplet is a logical choice. Installing and configuring server applications can be time consuming, and I was worried this would be a large chunk of work. But actually, the mail server was configured and up and running in a flash. I was pleasantly surprised by the software. In this article I will examine the steps that were necessary to get the mail server up and running.

Setting the host name

Before anything else, you might want to check the hostname configuration and make sure it points to your domain:
sudo vi /etc/hostname
The contents of this file should look something like this:
yourdomain.com
Mail servers typically use the hostname to identify the server they are running on.

Postfix

There are various mail servers to choose from, I went with postfix. The install process is documented in numerous tutorials, such as the ubuntu site:
https://help.ubuntu.com/lts/serverguide/postfix.html
or if you are on digital ocean as well, good news they have a guide as well:
https://www.digitalocean.com/community/tutorials/how-to-install-and-setup-postfix-on-ubuntu-14-04
I will not discuss the process in depth here, but only mention the steps I had to take.

First we need to install the software:

sudo apt-get install postfix
sudo dpkg-reconfigure postfix
Next we need to edit the main configuration file:
sudo nano /etc/postfix/main.cf
I had to fix the myhostname variable to point to my domain:
myhostname = yourdomain.com
Next I had to enable the virtual alias map:
virtual_alias_maps = hash:/etc/postfix/virtual
The virtual alias map is used to redirect email messages. This is a really powerful tool and I was surprised at how easy to use it is. Simply create / edit the text file for virtual emails:
sudo vi /etc/postfix/virtual
Normally linux creates an email address per user of the format [user]@[domain]. The email adresses mentioned here are virtual addresses. In other words, the user doesn't need to exist. Like I said in the introduction I wanted to forward incoming emails to my private email address.
someone@yourdomain.com ryans@privates.com
Entering the above line into /etc/postfix/virtual would forward incoming emails for the virtual user "someone" to Ryans privates. It is also possible to reroute messages for a virtual user to an actual linux user, like so:
virtual@yourdomain.com root
To load the changed configuration and (re)start the server:
sudo postmap /etc/postfix/virtual
sudo service postfix restart
And you can test the configuration by sending an email to someone@yourdomain.com.

sending mail

By default postfix will only be willing to send messages received from localhost. This works for my purposes, but you might want to send emails from other computers. I would caution against opening up the server entirely, because you will have opend a message relay usable by spammers. If your domain gets blocked by other mail servers due to spamming, then getting off of their blacklists can be difficult. An alternative would be to install a web client such as SquirrelMail or OpenWebMail:
https://help.ubuntu.com/community/Squirrelmail
https://help.ubuntu.com/community/OpenWebMail

Personally, I am sending mails from the command line through SSH. There is a whole bunch of command line email clients one can use:
http://tecadmin.net/ways-to-send-email-from-linux-command-line

Personally I chose mutt, because I found it to be the simplest to use. Editing the email body is most comfortable in a text editor:

vi message.txt
Sending the message:
mutt -s "subject line" ilikespam@spamfilter.com < message.txt
Attaching a file somewhere on the filesystem can be done with the -a flag:
mutt  -s "subject of email with attachment" ilikespam@spamfilter.com -a /somewhere/file.txt < message.txt
I find adding attachments to a file this way particularly efficient. It is far less complicated than in a webmail client. Of course there are command line flags for adding CC's Bcc's etc. Check the manual:
man mutt
Using the command line to send emails works really nice for me. I can easily send test mails to myself for review, before I send them to a possible client. Simply change the destination email on the command line and suddenly it is sent to the real destination. Main Page