Tutorial

How to Install and Configure Postfix as a Send-Only SMTP Server on Ubuntu 16.04

Published on April 30, 2016
How to Install and Configure Postfix as a Send-Only SMTP Server on Ubuntu 16.04
Not using Ubuntu 16.04?Choose a different version or distribution.
Ubuntu 16.04

Introduction

Postfix is a mail transfer agent (MTA), an application used to send and receive email. In this tutorial, we 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 that Postfix is installed on.

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 3rd party email service provider or running a full-blown SMTP server.

In this tutorial, you’ll learn how to install and configure Postfix as a send-only SMTP server.

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 this 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 Droplet when it was being created.

Step 1 — Installing Postfix

In this step, you’ll learn how to install Postfix. The most efficient way to install Postfix and other programs needed for testing email is to install the mailutils package.

First, update the package database:

  1. sudo apt-get update

Finally, install Postfix. Installing mailtuils will install Postfix as well as a few other programs needed for Postfix to function.

  1. sudo apt install mailutils

Near the end of the installation process, you will be presented with a window that looks exactly 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

After installation has completed successfully, proceed to step two.

Step 2 — Configuring Postfix

In this step, you’ll read how to configure Postfix to process requests to send emails only from the server on which it is running, that is, 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 that scenario 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.

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 that was 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 part 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 username and the domain part 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.

  1. sudo nano /etc/aliases

The full contents of the file on a default installation of Ubuntu 16.04 are as follows:

/etc/aliases
# See man 5 aliases for format
postmaster:    root

With that setting, system generated emails are sent to the root user. What you want to do is edit it so that those emails are rerouted to your email address. To accomplish that, edit the file so that it reads:

/etc/aliases
# See man 5 aliases for format 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 may now 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.

If your use case is to receive notifications from your server at a single address, emails being marked as spam is a major issue because you can whitelist them. However, if your use case is 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, this makes it difficult to send spam with an address that appears to originate from your domain. Doing 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 our products

About the authors
Default avatar
finid

author


Default avatar

staff technical writer

hi! i write do.co/docs now, but i used to be the senior tech editor publishing tutorials here in the community.


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 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!

In addition to the instructions above, create your own self-signed certs, use LetsEncrypt, or use purchased certificates/keys (make sure you use wildcard or certificates specific to your hostname/FQDN), and edit main.cf to include:

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/fullchain.pem (change to suit your system)
smtpd_tls_key_file=/etc/ssl/private/privkey.pem (change to suit your system)
smtp_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

smtpd_tls_CAfile=(if your cert and CAfile aren't rolled into a single file)

It cleared the warnings from the emails for me, and shows TLS was used.

Edit: Corrected to show smtp_use_tls=yes, not smtpd_use_tls=yes

From the comments it seems several people are having problems routing mail sent to their domain to an external mail service rather than locally. Perhaps the tutorial could be updated to show how to do this?

I think the problem is Postfix tries to use the local_transport delivery method, and delivers the mail to the local mail directory instead of resolving the domain to the remote email service.

To fix this, I changed mydestination in /etc/postfix/main.cf to:

mydestination = localhost.$mydomain, localhost

On my installation of Ubuntu 16.04 (not on your servers), the installation of mailutils didn’t produce a postfix configuration screen.

I have a problem when you have a server domain.com and a mail.domain.com and you try to install a send only postfix installation on domain.com.

When u set up, even when i set up as system.domain.com for postfix destination, and I try to send a email to somethingoruser@domain.com (which has a MX record pointing to mail.domain.com), it will say it doesn’t have a user.

No idea how to fix this.

Many thanks for this very useful tutorial!

This guide works too for Debian 8 Jessie, with one difference:

The package mailutils does not contain postfix, so the postfix package has to be installed too:

sudo apt-get install postfix

Two remarks:

  • I changed the droplet name of the system into my domain name, otherwise postfix will send mail from the original droplet name, which in most cases will be a non-existent domain. So the mydestination = line in /etc/postfix/main.cf will look like

mydestination = $mydomain, localhost.$mydomain, $mydomain

  • I found out that

sudo less /var/log/mail.log

gave me valuable information for troubleshooting.

Hola!, que tal? Instalé postfix para solo enviar correos, como lo marca en el tutorial, pero necesito que desde un backend con nodejs poder usar este servicio, pero no me deja, hay alguna configuración que me falta por hacer?

what are you saying. I need smtp credentials for it. Why do I need this procedure. Email to root only. I need to get smtp host

You need this command: sudo dpkg-reconfigure postfix to open configuration window.

Aannd for that part, “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.”, I asked @linuxbabe (twitter) and she kindly made a tutorial for us: https://www.linuxbabe.com/mail-server/postfix-send-only-multiple-domains-ubuntu

Why the need to install mailutils and not just postfix? Installing mailutils also installed MySQL, and I do not want MySQL on two of my three droplets for this project. I only need a means to either send mail or relay it to our GApps to send the mail. (From what I have read, there is no need for MySQL if using Postfix in a send-only situation.)

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!

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.