Install Postfix + SendGrid / Mandrill / Other External SMTP Server To Send Emails on Debian Linux

Must read:

Ferdian Alfianto

Ferdian Alfianto

Ferdian Alfianto is an Internet enthusiast, Mac Lover; likes using Wordpress, experimenting with Linux (especially Debian and Ubuntu), tinkering with pfSense routers, happy experimenting with LEMP (Linux, Nginx, MariaDB, PHP) and Redis. You can contact me here.

A few days ago, email notifications from the server that went to email, mostly went to the spam box. Also when I tried the forgot password feature of one of the WordPress-based sites, the email that was sent did not come in. One of the two is the weakness if the email is sent via the server, it is not reliable alias it is stable if the child's language is now :)
For that, I have to use External SMTP Server to replace the email system on the web server. There are many choices of External SMTP Server, which is quite a fancy name SendGrid, Mandrill by MailChimp and Amazon SES (Simple Email Service). Or you can just googling keywords "SMTP Server Services" or for those who hunt for free services can use keywords "Free SMTP Server Service". And here is a tutorial on installing Postfix using the SMTP Server to send more reliable emails.


  • This tutorial has been tried and worked on Debian 8 Linux, but it doesn't fail that it might work on Debian version 7 or other Debian-based linux variants, such as Ubuntu or KNOPPIX.
  • The SMTP Server that I successfully tried was SendGrid; but this tutorial can also be used using other external SMTP server
  • This tutorial cannot be used using Google's SMTP server (Gmail and Google Apps), if there is time I will write a separate tutorial.

Ok next step.

Dough Required

Before starting this tutorial, you already have:

  • Servers, either Dedicated Server or VPS, have Debian 8 Linux installed.
  • Fully Qualified Domain Name (FQDN) has been set on the server. Type the command hostname for the check.
  • Update the Linux packages with the command apt-get update && apt-get upgrade
  • You already have the SMTP server username & password.
  • Make sure the module libsasl2-modules already installed, if not already use the command apt-get install libsasl2-modules

NOTE: If your server is running Ubuntu, add sudo to execute the above commands.

Install Postfix

Type the command

apt-get install postfix mailutils

to start installing Postfix.

During the installation process, a dialog window will appear General type of mail configuration as follows.

choose Internet Site.

Then enter FQDN as below, for example

After the installation process is complete, open the Postfix configuration file with the command

nano /etc/postfix/

Confirm parameters myhostname filled in according to your FQDN

myhostname =

Configure SMTP Username & Password

Username and password will be saved file named passwd in the directory / etc / postfix / sasl /. Next, we will create a passwd file containing the SMTP server username & password which Postfix will use later.

If You Use SendGrid

Login to SendGrid, select menu Settings -> API Keys. Then click the button Create API Key.

Give the name of the API Key that will be created. And on choice API Key Permissions, choose Full Access. See the picture below.

Then click the button Create & View. On the next page, the API Key will appear, copy and paste the API Key into notepad.

Next we will create a file passwd with orders

nano / etc / postfix / sasl / passwd

and enter the API Key that we created earlier, with a format like this

[]: 587 apikey: APIkey code

NOTE: apikey is the SMTP username, leave it like that. Moderate code APIkey is the API Key that we created above. While 587 is the port used by SendGrid.

Then we will create a file hash db for Postfix by running the command

postmap / etc / postfix / sasl / passwd

If successful, a file will appear passwd.db in the directory / etc / postfix / sasl /. Can check with orders ls -l / etc / postfix / sasl /

To make the passwd and passwd.db files more secure, we will set them up so that only root can read and modify them; and also we will change the ownership of these files to root. Use this command:

chown root: root / etc / postfix / sasl / passwd /etc/postfix/sasl/passwd.db
chmod 0600 / etc / postfix / sasl / passwd /etc/postfix/sasl/passwd.db

Next, we will configure the /etc/postfix/, so that Postfix can use the SendGrid SMTP server. Type the command

nano /etc/postfix/

Change the relayhost according to SendGrid parameters

# SMTP host from SendGrid relayhost = []: 587

Then at the end of the file /etc/postfix/, add this parameter

# enable SASL authentication smtp_sasl_auth_enable = yes # turn off anonymous authentication methods smtp_sasl_security_options = noanonymous # sasl location passwd smtp_sasl_password_maps = hash: / etc / postfixls etc / certificates STTP_tl_tltlScAt 1TP_tl_tile_certs /ca-certificates.crt

Save the changes above and restart Postfix with the command

service postfix restart


/etc/init.d/postfix restart

Next, test Postfix to send an email with the command

echo "email body" | mail -s "This is Email Title" -a "From:"

NOTE: Over with any email address. And change with the email address that will be used to receive email (use a valid email, so we know email has been entered or not).

If You Use Another SMTP Server

In principle, the configuration is the same, all you need to change is the / etc / postfix / sasl / passwd in order to match the SMTP server address, port, username and password

[]: 587 username: password

And also change relayhost on file /etc/postfix/

relayhost = []: 587

Don't forget to create a hash db with orders

postmap / etc / postfix / sasl_passwd

And also restart Postfix

service postfix restart


/etc/init.d/postfix restart

Good luck!


Latest articles:

MongoDB logo

Easy to Install MongoDB on Ubuntu 20.04

This tutorial explains how to install and configure MongoDB Community Edition on Ubuntu 20.04. MongoDB is a free, open-source document database. Belongs to the so-called database family

Related article:

MongoDB logo

Easy to Install MongoDB on Ubuntu 20.04

This tutorial explains how to install and configure MongoDB Community Edition on Ubuntu 20.04. MongoDB is a free, open-source document database. Belongs to the so-called database family

rocket nginx

Rocket-Nginx + WP-Rocket: What are the Benefits?

What is Rocket-Nginx? Rocket-Nginx is a configuration add-on to Nginx for the WordPress cache plugin, WP-Rocket. The developer claims that by injecting the Rocket-Nginx configuration, the


The World's Cheapest Domain Hunt!

The cheapest domain, who doesn't love getting it? Yes, even the author of this blog loves to hunt for cheap domains :). Frequently questions are asked to the author,