Postfix as a Send-Only SMTP Server

Let's show you how to install and configure Postfix as a send-only SMTP server on Debian 9 +

Postfix as a Send-Only SMTP Server
postfix

Prerequisites

To follow this tutorial, you will need:

  • One Debian 9 +  server and a sudo non-root user.

  • You will need a valid domain name.

Note that your server’s hostname should match your domain or subdomain. You can verify the server’s hostname by typing 

hostname 

at the command prompt. To update this use:

sudo nano /etc/hostname

to update FQDN, use :

sudo nano /etc/hosts

Step 1 — Installing Postfix

In this step, you’ll learn how to install Postfix. You will need two packages: mailutils, which includes programs necessary for Postfix to function, and postfix itself.

First, update the package database:

sudo apt update && sudo upgrade -y

Next, install mailtuils:

sudo apt install mailutils

                                                             Finally, install postfix:

sudo apt install postfix

Near the end of the installation process, you will be presented with a window that looks like the one in the image below. The default option is Internet Site. That’s the recommended option for this tutorial, so press TAB, then ENTER.

Select Internet Site from the menu, then press TAB to select <Ok>, then ENTER

After that, you’ll get another window just like the one in the next image. The System mail name should be the same as the name you assigned to the server when you were creating it. If it shows a subdomain like subdomain.example.com, change it to just example.com. When you’ve finished, press TAB, then ENTER.

Enter your domain name, then press TAB to select <Ok>, ENTER

You now have Postfix installed and are ready to modify its configuration settings

Step 2 — Configuring Postfix

In this step, you’ll configure Postfix to process requests to send emails only from the server on which it is running, i.e. from localhost.

For that to happen, Postfix needs to be configured to listen only on the loopback interface, the virtual network interface that the server uses to communicate internally. To make the change, open the main Postfix configuration file, we use nano

sudo nano /etc/postfix/main.cf

With the file open, scroll down until you see the following section:

/etc/postfix/main.cf
. . .
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
. . .

Change the line that reads inet_interfaces = all to inet_interfaces = loopback-only:

/etc/postfix/main.cf
. . .
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only
. . .

Another directive you’ll need to modify is mydestination, which is used to specify the list of domains that are delivered via the local_transport mail delivery transport. By default, the values are similar to these:

sudo nano /etc/postfix/main.cf
. . .
mydestination = $myhostname, example.com, localhost.com, , localhost
. . .

The recommended defaults for this directive are given in the code block below, so modify yours to match:

sudo nano /etc/postfix/main.cf
. . .
mydestination = $myhostname, localhost.$your_domain, $your_domain
. . .

Save and close the file.

Note: If you’re hosting multiple domains on a single server, the other domains can also be passed to Postfix using the mydestination directive. However, to configure Postfix in a manner that scales and that does not present issues for such a setup involves additional configurations that are beyond the scope of this article.

Finally, restart Postfix.

sudo systemctl restart postfix

    Step 3 — Forcing the "from" address

     In this step, you will make config changes so that sender addresses from both local originated, and relayed SMTP mail traffic is not blank:

    Finally, restart Postfix.

    sudo nano /etc/postfix/main.cf
    /etc/postfix/main.cf


    . . .
    sender_canonical_classes = envelope_sender, header_sender sender_canonical_maps = regexp:/etc/postfix/sender_canonical_maps smtp_header_checks = regexp:/etc/postfix/header_check

    . . .

    Rewrite envelope address from email originating from the server itself

    sudo nano /etc/postfix/sender_canonical_maps
    /etc/postfix/sender_canonical_maps

    /.+/    [email protected]

    Rewrite from address in SMTP relayed e-mail

    sudo nano /etc/postfix/header_check
    /etc/postfix/header_check

    /From:.*/ REPLACE From: [email protected]

    That's it, finally restart Postfix.

    sudo systemctl restart postfix

      Step 4 — Testing the SMTP Server

      In this step, you’ll test whether Postfix can send emails to an external email account using the mail command, which is part of the mailutils package you installed in Step 1.

      To send a test email, type:

      echo "This will be the body of the email" | mail -s "NoReply Subjet" [email protected]

      Now check the email address where you sent the test message. You should see the message in your Inbox. If not, check your Spam folder.