Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How To Install and Configure Postfix on Ubuntu 18.04

Table Of Contents

    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [Install Postfix](#install-postfix
    – [Tweak the Postfix Configuration](#tweak-the-postfix-configuration
    – [Map Mail Address to Linux Accounts](#map-mail-address-to-linux-accounts
    – [Adjust the Firewall](#adjust-the-firewall
    – [Setting up the Environment to Match the Mail Location](#setting-up-the-environment-to-match-the-mail-location
    – [Install and Configure the Mail Client](#install-and-configure-the-mail-client
    – [Initialize the Maildir and Test and Client](#initialize-the-maildir-and-test-and-client
    – [ Conclusion](#conclusion

    ## Introduction

    [Postfix]( is an open-source Mail Transfer Agent. It can be used to deliver email on a Linux system. It is estimated that around one-quarter of the public mail servers on the internet run Postfix.

    ## Prerequisites

    You must have to install the [initial server setup on Ubuntu 18.04](

    ## Install Postfix

    Installation of postfix is very simple because it is included in Ubuntu’s default repositories. First of all, update your local apt package cache and then install the software. You will be passing in the DEBIAN_PRIORITY=low environmental variable into our installation command in order to answer some additional prompts.

    $ sudo apt update
    $ sudo DEBIAN_PRIORITY=low apt install postfix

    If you want to fill the prompts correctly use the following information.

    ## The general type of mail Configuration

    In general type of mail configuration we will use Internet Site So this matches our needs.

    ## System mail name

    This is the base domain used to construct a valid email address if only the account portion is given.

    ## Root and postmaster mail recipient

    This is the Linux account and it will be directed mail addressed to root@ and postmaster@. In this case, use your primary account.

    ## Other destinations to accept mail for

    This means the mail destinations that postfix instance will accept. If you want to add any other domain then that server will be responsible for receiving and adding those if not the default should be fine.

    ## Tweak the Postfix Configuration

    We have to adjust some settings which are not prompt us by the package.

    At first, we have to set the mailbox which will use the Maildir format. It separates messages into individual files that are then moved between directories based on user action. Another way is the mbox format. It stores all messages within a single file.

    You have to set the home_mailbox variable to Maildir/ which will create a directory structure under that name within the user’s home directory. To set configuration settings use postconf command. To configure home_mailbox use the below command.

    $ sudo postconf -e ‘home_mailbox= Maildir/’

    Next, we can set the location of the virtual_alias_maps table. This table maps arbitrary email accounts to Linux system accounts. You can create this table at /etc/postfix/virtual. Again we can use the postconf command.

    $ sudo postconf -e ‘virtual_alias_maps= hash:/etc/postfix/virtual’

    ## Map Mail Address to Linux Accounts

    Next step, you have to set up the virtual maps in your text editor.

    $ sudo vi /etc/postfix/virtual

    In virtual alias map table, left side you can set any number of addresses that you wish to accept email for. And then separated by whitespace, enter the Linux user you’d like that mail delivered to.
    Suppose, if you want to like to accept email at and and would like to have those emails delivered to the gautami Linux user then you can set up your file like this:

    contact@testdkim gautami
    admin@testdkim gautami

    Once you have mapped all of the addresses to the appropriate server accounts, save and close the file.

    You can apply the mapping by entering the below command.

    $ sudo postmap /etc/postfix/virtual

    Restart the Postfix process and be sure that all of our changes have been applied.

    $ sudo systemctl restart postfix

    ## Adjust the Firewall

    If you are running the UFW firewall, as configured in the initial server setup guide, you have to allow an exception for postfix. You can allow connections to the service by entering the below command.

    $ sudo ufw allow Postfix

    Now the Postfix server component is installed. Next, you have to set up a client which can handle the mail that Postfix will process.

    ## Setting up the Environment to Match the Mail Location

    You have to make sure that MAIL environmental variable set correctly before you install a client. Then the client will inspect this variable to figure out where to look for the user’s mail. To set variable irrespective of how you access your account. You need to set a variable in a few different locations. We will add it to /etc/bash.bash.bashrc and a file within /etc/profile.d to make sure each user has this configured.

    Use the below command to add the variable to these files.

    $ echo ‘export MAIL=~/Maildir’ | sudo tee -a /etc/bash.bashrc | sudo tee -a /etc/profile.d/

    You can get the /etc/profile.d/ file to read the variable into your current session.

    $ source /etc/profile.d/

    ## Install and Configure the Mail Client

    If you want to interact with the mail being delivered, you have to install the s-nail package. This is the variant of the BSD xmail client. It is feature-rich and it can handle the Maildir format correctly and also more backward compatible. The GNU version of mail has a limitation i.e. always saving read mail to the mbox format irrespective of the source format.

    Follow the below command to install the s-nail package.

    $ sudo apt install s-nail

    You must adjust a few things i.e. open the /etc/s-nail.rc file in your vi editor.

    $ sudo nano /etc/s-nail.rc

    Add the following script at the end of the file.

    set emptystart
    set folder=Maildir
    set record=+sent

    This script allows the client to open even with an empty inbox. This will set the Maildir directory to the internal folder variable and then use this to create a sent mbox file within that, for sorting sent mail.
    Once you finished then save and close the file.

    ## Initialize the Maildir and Test and Client

    Now you can test the client.

    ## Initializing the Directory Structure

    If you want to create the Maildir structure within your home directory then you can send yourself an email. You can do it by using s-nail command. Because the file which is sent will only be available once the Maildir is created, You must disable writing to that for your initial email. You can do this by passing the -Snorecord option. Send the email by adding a string to the s-nail command. Rewrite the command to mark your Linux user as the recipient.

    $ echo ‘init’ | s-nail -s ‘init’ -Snorecord sam

    This may only appear when you are sending this first message. You can check to ensure that the directory was created by looking for our ~/Maildir directory.

    $ ls -R ~/Maildir

    You have to see the directory structure has been created and that a new message file is in the ~/Maildir/new directory.


    It seems like your mail has been delivered.

    ## Managing Mail with the Client

    You can check your mail using the client as follows.

    $ s-nail

    You will get output like this.


    Now you have to press ENTER to display your message.


    If you want to go back then type h, and then press ENTER.

    ? h



    If you want to delete it, then you can do it by typing d, and then press ENTER.

    ? d

    To get the terminal type q, and then ENTER.

    ? q

    ## Sending Mail with the Client

    You can test it sending mail by entering a message in the text editor.

    $ vi ~/test_message

    Enter some text inside the editor to email.

    This is a test. Please confirm receipt!

    You can add the message to the s-nail process using the cat command. This will send the message as your Linux user by default. You can adjust the “from” field with the -r flag if you want to modify that value to someone else.

    $ cat ~/test_message | s-nail -s ‘Test email subject line’ -r from_field_account


    -s : The subject line of the email.
    -r : An optional change to the “From” field of the email. This option allows you to override this. The account to send the email to. Use the valid email account you have to access to.

    You can view your sent messages within your s-nail client by using below command.

    $ S-nail

    If you want to see the sent messages only then type the below command.

    ? file +sent

    You can use the same commands for incoming mail too.

    ## Conclusion

    Now you have Postfix configured on your Ubuntu 18.04 server.