For quite a while, having some new server set up, I would find that some MTA (Mail Transfer Agent) is already running and usually requires not much of a configuration. However, I have seen quite a few installation of Debian 9 (Stretch) this year, where it is not the case, at least when some images are installed on a VPS/VDS.
So just as another note here, for myself or anyone else who might find this useful, I will describe how to set up both MTA and MDA (Mail Delivery Agent) in such case. Why would you need this? Well, for starters you might want to be receiving some mails from your application or processes (such as unattended upgrades process for example) and being able to read such mail or to forward it somewhere else.
For MTA you would probably be choosing between Exim and Postix. Both are good, but from the security standpoint you might probably rely on Postfix a bit more. If you need a lot of flexibility, then it might have to be Exim. You can find a lot of comparisons between the two, and decide for yourself. In this example we will have a look at the basic configuration of Postfix, for the server on which you do not intend to receive any external emails and will be just delivering email locally within the host or sending it somewhere.
Before you begin, you might want to make a seemingly unrelated change. When you have your mail server running, you might notice that /var/log/ directory has more than just one mail log - it has mail.log and mail.info. While they might seem identical, they could also have different content. This is because by default rsyslog is set up to log messages on all levels to mail.log and then split info/warning/error messages into separate files. If you don't really want that (or you might want to actually have aditional separate logs for warnings and errors only), then you need to edit /etc/rsyslog.conf file by commenting out the following line:
Then restart or reload rsyslog:
sudo service rsyslog restart
Now we can install Postfix:
sudo apt-get install postfix
In the process you will be shown a screen asking you to select the type of configuration. Choose "Internet site" and confirm by selecting "OK". Then you will be asked for the "System mail name". You can use your full hostname (with the domain part, such as myhost.mydomain.ext). Once that is also confirmed, you should see installation messages. You might also see a following warning "/etc/aliases exists, but does not have a root alias." - more on that below.
Now let's change the configuration so the mail server is not exposed to the outside world and only listens to your localhost interface. Open /etc/postfix/main.cf and scroll down untill you see something like:
Replace that with:
inet_interfaces = loopback-only
You might also want to make sure that "mydestination" is set as follows:
mydestination = $myhostname, localhost.$mydomain, $mydomain
Once the configuration file is saved, restart the service:
sudo service postfix restart
If you want all mails for the "root" user on your server to be forwarded to some external domain, you will also need to edit /etc/aliases file and add the line similar to the one below:
After that file is saved, run the following command to make the new alias active:
If you don't have "mail" utility installed on your system yet, then install one. You can choose between a few packages (mailutils, bsd-mailx, etc.), but for the simplicity we will use mailutils in this example:
sudo apt-get install mailutils
Finally send a test email and see if it gets delivered to your mailbox:
echo "Hi, this is a test message." | mail -s "This is the subject" root
This should be sufficient for the purpose of processing mail locally and forwarding it as you like, without accepting external emails.