Tutorial

How To Install and Configure Postfix as a Send-Only SMTP Server on Debian 10

Published on July 8, 2019
How To Install and Configure Postfix as a Send-Only SMTP Server on Debian 10
Not using Debian 10?Choose a different version or distribution.
Debian 10

Introduction

Postfix is a mail transfer agent (MTA), an application used to send and receive email. In this tutorial, you will install and configure Postfix so that it can be used to send emails by local applications only — that is, those installed on the same server as Postfix.

Why would you want to do that?

If you’re already using a third-party email provider for sending and receiving emails, you do not need to run your own mail server. However, if you manage a cloud server on which you have installed applications that need to send email notifications, running a local, send-only SMTP server is a good alternative to using a third-party email service provider or running a full-blown SMTP server.

In this tutorial, you’ll install and configure Postfix as a send-only SMTP server on Debian 10.

Note: As of June 22, 2022, DigitalOcean is blocking SMTP for all new accounts. As a part of this new policy, we have partnered with SendGrid so our customers can still send emails with ease. You can learn more about this partnership and get started using SendGrid by checking out our DigitalOcean’s SendGrid Marketplace App.

Prerequisites

To follow this tutorial, you will need:

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. The output should match the name you gave the server when it was being created.

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:

  1. sudo apt update

Next, install mailtuils:

  1. sudo apt install mailutils

Finally, install postfix:

  1. 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:

Initial Config Screen

Press ENTER to continue.

The default option is Internet Site, which is preselected on the following screen:

Config Selection Screen

Press ENTER to continue.

After that, you’ll get another window to set the System mail name:

System Mail Name Selection

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.

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 using nano or your favorite text editor:

  1. 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:

/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:

/etc/postfix/main.cf
. . . mydestination = $myhostname, localhost.$mydomain, $mydomain . . .

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.

  1. sudo systemctl restart postfix

Step 3 — 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:

  1. echo "This is the body of the email" | mail -s "This is the subject line" your_email_address

In performing your own test(s), you may use the body and subject line text as-is, or change them to your liking. However, in place of your_email_address, use a valid email address. The domain can be gmail.com, fastmail.com, yahoo.com, or any other email service provider that you use.

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.

Note that with this configuration, the address in the From field for the test emails you send will be sammy@example.com, where sammy is your Linux non-root username and the domain is the server’s hostname. If you change your username, the From address will also change.

Step 4 — Forwarding System Mail

The last thing we want to set up is forwarding, so you’ll get emails sent to root on the system at your personal, external email address.

To configure Postfix so that system-generated emails will be sent to your email address, you need to edit the /etc/aliases file. Open that file now:

  1. sudo nano /etc/aliases

The full contents of the file on a default installation of Debian 10 are as follows:

/etc/aliases
mailer-daemon: postmaster
postmaster: root
nobody: root
hostmaster: root
usenet: root
news: root
webmaster: root
www: root
ftp: root
abuse: root
noc: root
security: root

The postmaster: root setting ensures that system-generated emails are sent to the root user. You want to edit these settings so these emails are rerouted to your email address. To accomplish that, add the following line below the postmaster: root setting:

/etc/aliases
mailer-daemon: postmaster
postmaster:    root
root:          your_email_address
. . .

Replace your_email_address with your personal email address. When finished, save and close the file. For the change to take effect, run the following command:

  1. sudo newaliases

You can test that it works by sending an email to the root account using:

  1. echo "This is the body of the email" | mail -s "This is the subject line" root

You should receive the email at your email address. If not, check your Spam folder.

Conclusion

That’s all it takes to set up a send-only email server using Postfix. You may want to take some additional steps to protect your domain from spammers, however.

If you want to receive notifications from your server at a single address, then having emails marked as Spam is less of an issue because you can create a whitelist workaround. However, if you want to send emails to potential site users (such as confirmation emails for a message board sign-up), you should definitely set up SPF records and DKIM so your server’s emails are more likely to be seen as legitimate.

If configured correctly, these steps make it difficult to send Spam with an address that appears to originate from your domain. Taking these additional configuration steps will also make it more likely for common mail providers to see emails from your server as legitimate.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
finid

author



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
4 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Hi, Never received an email! I’ve tried difference email and settings.

Hi Kathleen,

doing all steps you described above. But when sending a short mail (you echo … | mail -s “subject” … command no mail will be send. I get some error messages in /var/log/mail.log: May 21 17:04:19 bigserver postfix/pickup[11579]: 3E2B32006A: uid=1000 from=rhartwig@bigserver.scons.de May 21 17:04:19 bigserver postfix/cleanup[11594]: 3E2B32006A: message-id=20200521150419.3E2B32006A@bigserver.scons.de May 21 17:04:19 bigserver postfix/qmgr[11580]: 3E2B32006A: from=rhartwig@bigserver.scons.de, size=411, nrcpt=1 (queue active) May 21 17:04:49 bigserver postfix/smtp[11596]: connect to gmail-smtp-in.l.google.com[64.233.167.27]:25: Connection timed out May 21 17:04:49 bigserver postfix/smtp[11596]: connect to gmail-smtp-in.l.google.com[2a00:1450:400c:c0a::1a]:25: Cannot assign requested address May 21 17:05:19 bigserver postfix/smtp[11596]: connect to alt1.gmail-smtp-in.l.google.com[209.85.233.27]:25: Connection timed out May 21 17:05:19 bigserver postfix/smtp[11596]: connect to alt1.gmail-smtp-in.l.google.com[2a00:1450:4010:c03::1b]:25: Cannot assign requested address May 21 17:05:49 bigserver postfix/smtp[11596]: connect to alt2.gmail-smtp-in.l.google.com[142.250.4.27]:25: Connection timed out May 21 17:05:49 bigserver postfix/smtp[11596]: 3E2B32006A: to=rainer.hartwig@gmail.com, relay=none, delay=90, delays=0.04/0.02/90/0, dsn=4.4.1, status=deferred (connect to alt2.gmail-smtp-in.l.google.com[142.250.4.27]:25: Connection timed out)

Can you help?

thanks in advance

regards Rainer

Thank you very much. It works very well and it is enough for my purpose.

However, I did not have to do “If it shows a subdomain like subdomain.example.com, change it to just example.com” : my server host’name is set up with a subdomain, and I let it in the System mail name. It still works.

When you install mailutils with apt, it also installs exim, which, if I understand correctly, is another full-fledged MTA just like Postfix. So, given our goal is to install Postfix, by following this guide will we end up with both Postfix AND exim installed? Is exim also enabled by just installing it? Should we disable it, or do something? I don’t like the idea to install another full MTA besides the one that I’m actually going to use…

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel