How to Configure Postfix to Send an Email using SMTP on Linux VPS
Posted in: Email, Hosting, Linux, PHP, Ubuntu, VPS

How to Configure Postfix to Send an Email using SMTP on Linux VPS

If your website is running on Linux VPS, then you may want to install and configure Postfix for sending an email.

Postfix is an open-source, free, and the most widely used Mail Transfer Agent (MTA) that routes and delivers an email.

In this guide, we will install and set up Postfix to make use of Simple Mail Transfer Protocol (SMTP) for sending an email.

This will allow native mail functions in any programming language like mail() in PHP to send emails directly using SMTP configured with Postfix.

Here, we specifically configure the SMTP of Gmail with Postfix. But this would work with any SMTP provider. Also, we use Ubuntu as a Linux distribution.

Installing Postfix on Linux

First of all, make sure to update the packages list by running the following command:

> sudo apt-get update

Next, install the “mailutils” package in order to use the mail utility and test out the email:

> sudo apt-get install mailutils

Now, we are ready to install the “postfix” package:

> sudo apt-get install postfix

You may not need to install the “postfix” separately when you install the “mailutils”.

After running the install command, it will ask for “General type of mail configuration”. Set it to “Internet Site”.

Press the TAB key to highlight “Ok” and hit the Enter key to continue.

Next, it will ask for “System mail name”. Here, you will need to input your website domain name like “”.

Once again, press the TAB key and continue.

Configuring Postfix for SMTP

To configure the Postfix for SMTP, we will need to edit its configuration file:

> sudo nano /etc/postfix/

In this file, find the line with “relayhost” (“Ctrl + W” to search “relayhost”) and add its value to “[]:587”. So, it will look like this:

relayhost = []:587

Here, we are configuring Gmail SMTP, so the host is “” and the port number is 587.

The SMTP host and SMTP port number can vary depending upon the SMTP provider.

Now, find the following block of lines:

# TLS parameters

smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

Here, we will need to comment these two lines in the above block:

#smtpd_tls_security_level = may
#smtp_tls_security_level = may

So, the block of lines would look like this:

# TLS parameters

smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

Also, at the bottom of the file, add the following block of lines:

# Enable SASL authentication
smtp_sasl_auth_enable = yes
# Disallow methods that allow anonymous authentication
smtp_sasl_security_options = noanonymous
# Location of sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
# Enable STARTTLS encryption
smtp_tls_security_level = encrypt
# Location of CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

After adding these lines, save the configuration file with Ctrl + X and Y (for nano editor).

Next, we will create the following file to store the SMTP credentials of the Gmail account:

> sudo nano /etc/postfix/sasl/sasl_passwd

Enter the following in this file:


In the above line, you will also need to replace the email: “” and the password: “YourGmailPassword” with the actual email and password of your Gmail account to use for sending an email.

Then you can save this file (Ctrl + X and Y).

Now, for Gmail, we will need to do one additional step which is to allow less secure apps.

Make sure the “Allow less secure apps” is set to “ON” for the Gmail account that you want to use for sending emails via SMTP.

Next, run the following command:

> sudo postmap /etc/postfix/sasl/sasl_passwd

Also, set the ownership and permissions of these files by running the following commands:

> sudo chown root:root /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db
> sudo chmod 0600 /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db

Finally, restart Postfix to load the new configuration:

> sudo systemctl restart postfix

Testing Configuration by Sending a Test Email

Now, you can test sending an email with the following command:

> echo "This email confirms that Postfix is working" | mail -s "Testing Posfix"

Here, “” is the email of the receiver. So, you would receive an email from “” that we configure with SMTP.

Also, you can test sending emails by using native mail functions like the “mail()” in PHP.

You can check the logs of the Postfix from this file:


If you do not receive an email when using Gmail SMTP, then you can try to make sure to allow less secure apps and allow access for new devices.

Also, you can try changing your Gmail password and re-update the “sasl_passwd” file with this new password.

In case you still do not receive an email, then make sure your VPS provider is not blocking the SMTP port. If that is the case, then you can ask them to unblock the SMTP port numbers like 587, 25, etc.

For example, some VPS providers like Vultr block SMTP ports by default but they also unblock them when you submit a support ticket asking them to unblock SMTP ports.

If you face any issues when installing or configuring the Postfix with SMTP, then you can also reach out to us using our contact form.

Back to Top