In this guide, we will go over how to install and configure Postfix and s-nail to send and receive mail on your Ubuntu 18.04 server. For this guide, you will need to connect to your server using SSH as root or a superuser. In order to continue, a Hostwinds Cloud, VPS or Dedicated Server should be ready for use.
As always, before making any changes to your server or continuing with this guide, we highly suggest you have a good working backup of your server.
As always, make sure your server is up to date.
Step 1: Install and configure Postfix
The first step is to install Postfix. We are using
DEBIAN_PRIORITY=low in order to see more options during installation.
Type the following into the terminal
sudo DEBIAN_PRIORITY=low apt install postfix -y
Next, we will want to configure some options:
Step 2: Tweak Postfix
There are some items that were not set up during the installation of the package that we will be addressing below.
We will be creating a directory structure under the user’s home directory. Set this up by typing
sudo postconf -e 'home_mailbox= Maildir/'
We will then specify where the virtual_alias_maps table will be stored:
sudo postconf -e 'virtual_alias_maps= hash:/etc/postfix/virual'
We are now going to set up the virtual maps file. You can open this in your favorite text editor. In this tutorial, we are going to use nano.
sudo nano /etc/postfix/virtual
The formatting for this file is that the address on the left is an email address you would like to receive mail for, then a blank space, then the Linux user that you would like the mail delivered to. Your file with your email addresses and Linux users will look something like the following:
Save the file and exit the editor, and apply our mapping changes by typing
sudo postmap /etc/postfix/virtual sudo systemctl restart postfix
The next step is to allow Postfix through the firewall. In this example, we are using ufw, and you can use the following command:
sudo ufw allow postfix
Step 3: Installing s-nail
A prerequisite of installing a mail client is going to be setting the MAIL variable. The mail client checks this variable to figure out where to get the user’s mail from. For this to work no matter how the user accesses their account, you will need to set this variable in multiple locations:
echo 'export MAIL=~/Maildir' | sudo tee -a /etc/bash.bashrc | sudo tee -a /etc/profile.d/mail.sh
You will also want to source the
/etc/profile.d/mail.sh file into your current session:
Next, we will install the s-nail package:
sudo apt install s-nail
After this is complete, open
/etc/s-nail.rc in your text editor of choice:
sudo nano /etc/s-nail.rc
At the bottom of the file, add the following lines:
set emptystart set folder=Maildir set record=+sent
Step 4: Initialize The Directory Structure
The best way to create the Maildir structure is to send yourself an email. You will use the s-nail command with the -Snorecord option. Change the following command to match the user you are wanting to use:
echo 'init' | s-nail -s 'init' -Snorecord user
To view all commands possible, you can view the help page for s-nail: