E-mail is one of the most basic messaging methods used since the development of the internet. This has led to the development of many messaging tools and protocols. There are protocols for the compatible operation of systems with different architectures.

E-mail servers perform tasks such as not losing user e-mails, and assisting users in accessing their e-mails when they're online. The tools that provide e-mail transmission on local intranets and the internet are called Mail Transfer Agents (MTA).

Here's how to install and set up a Postfix mail server on a Debian Linux installation.

Step 1: Postfix Mail Server Installation on Debian

In many cases, Postfix comes to the fore due to its ease of use, few settings, and the high number of uses. Use the following command to install Postfix on your Debian-based Linux distribution:

        Internet Sitesudo apt install postfix

If you've Sendmail installed on your system, it would be better to uninstall it before installing Postfix.

        sudo apt remove sendmail
    

During installation, Postfix will ask you some questions. Select Internet Site for the first incoming request and enter the domain name you want in the second prompt.

select Internet site as the generic type for Postfix configuration
add your domain name to the system mail name

Like most Linux software, Postfix stores its settings in files. This is great as you can make any changes you want to Postfix by simply editing text files. Postfix settings are generally located in the main.cf file. Just to be on the safe side, you should always create a backup of the default main.cf file.

Step 2: Configurations for Postfix Mail Server

First of all, open the main.cf file with a text editor of your choice and change its content as you want. There is a lot of basic information inside the file, such as mydomain (domain name of the machine) and myhostname (full name of the SMTP server). Tweak the settings to make the server work. You can refer to the example below to get an idea.

        vim /etc/postfix/main.cf
    
content of main-cf file for postfix configuration

Step 3: Sending Mail With Postfix

To send your first mail using the Postfix server, use the commands below. These commands are for adding your IP address and domain name to the hosts file.

        hostnamectl set-hostname mail.fatih.com
echo "192.168.0.100 fatih.lab mail.fatih.lab" >> /etc/hosts
reboot

Reboot your system to make sure the settings take effect. After the reboot, check the contents of your hostname file with the command below.

        hostname && cat /etc/hostname && hostname -s


# Output
mail.fatih.com
mail.fatih.com
mail

Then check your port number using the following command to make sure everything is in order:

        netstat -tlpn
checking hostname file and netstat status

Now you can compose your first message and see how things are going.

Send your first email with the command below:

        echo "my first mail content" | mail -s "Hello Postfix" root

After this step, open the Maildir folder specified in the main.cf file to verify if everything is fine. If all went well, the email you sent should be here.

        cat /Maildir/new/[Press Tab]
first mail with postfix server

Step 4: IMAP and Dovecot Configuration

If you've ever had an email server service, you've probably heard of email protocols before. These protocols are POP and IMAP. The choice you make has a huge impact on your sending, receiving, and other email transmission flows. While POP (Post Office Protocol) is the most popular type of email protocol, IMAP (Internet Message Access Protocol) is the protocol of choice for most users these days.

Below you will find an example of sending mail with Postfix and IMAP. But before that, it's important to have at least a superficial knowledge of IMAP.

IMAP acts as a bridge between your email client and your email server. E-mail servers allow you to send and receive emails. However, IMAP allows you to preserve your emails on the server until you remove them manually. When you log in to an email client, such as Gmail or Outlook, the client uses IMAP to connect to the email server.

Also, IMAP has many advantages. For example, you can access your email messages from as many different devices as you want and from anywhere. It also downloads the relevant message only when you click on it.

Email attachments are not automatically downloaded with IMAP. So you can check your messages much faster and have control over which file attachments you want to open.

As the world moves more and more towards mobile, IMAP naturally becomes more popular. The proliferation of smartphones, laptops, tablets, and other devices is making the demand for IMAP more intense than ever before.

Configuring Dovecot

With Dovecot, it is possible to send network mail transfers using POP3 or IMAP. The first thing you need to do to get it working is to install it on your system with the following command:

        sudo apt install dovecot-core dovecot-imapd
    

After the installation, there will be some files in the /etc/dovecot folder. You need to make various adjustments to these files and configure Dovecot.

First, you need to establish a connection between your Maildir folder and Dovecot. The mail_location line specifies the folder to store the mails. Replace the expression here with Maildir as follows. To do this, open the 10-mail.conf file with any editor you want.

        vim /etc/dovecot/conf.d/10-mail.conf

And change the line starting with mail_location to:

        mail_location = maildir:~/Maildir

Now you will establish a connection between Postfix and Dovecot. For this, open the 10-master.conf file in the same way and change the codes below the Postfix smtp-auth comment:

        vim /etc/dovecot/conf.d/10-master.conf

Change the following lines in the file:

        unix_listener /var/spool/postfix/private/auth {
  mode = 0666
  user = postfix
  group = postfix
 }
configuration postfix smtp auth block

Dovecot works on port 143 by default. To check this, restart the dovecot service and check the port number with the command below:

        netstat -tlpn
dovecot check 143 port with netstat

Now that all is well, it's time to send a real email. For this, create a new user using the commands below:

        adduser testuser

Then connect to port 25 using the nc command.

        nc localhost 25

The contents of your terminal login screen may vary slightly but that is normal. You can use Ctrl + C to exit and retry if you bump into any error. The command you should use at this stage is:

        ehlo localhost

You are now at the stage of editing the email content. The command structure you need to enter at this stage is:

        mail from: root
rcpt to: testuser
data
subject: testsubject
my mail content
.
quit
first-mail-content-with-dovecot-postfix-1

There is one last place to check whether the e-mail transmission was successful. By configuration, this email needs to be in the Maildir folder. To do this, check whether the e-mail you sent exists in this folder with the following command:

        cat /home/testuser/Maildir/new/[Press Tab]
dovecot and postfix first mail content

As you can see, with Dovecot IMAP and Postfix, you have successfully sent the desired content to testuser.

Why Use Postfix on Linux Servers?

As you can see from the examples, using Postfix is very practical and easy. Postfix is very flexible for mail server administrators and developers. Moreover, it's compatible with many tools such as SQL, MySQL, Cyrus, LDAP, SASL, TSL, and SSL. Compared to other MTAs, it performs better even on systems with heavy traffic and several users.

Postfix runs quite smoothly on not only Linux but Unix and Windows as well. Even technology companies with high-tech servers such as Microsoft, Google, and Amazon have used Postfix in many areas. Moreover, it is much more successful in terms of security, especially when compared to Sendmail. For these reasons, the Postfix mail architecture is still preferred by mail server administrators even after 20 years.