Install and Configure Postfix with Gmail SMTP for Perfect Mailing System

Learn to install and configure Postfix as SEND-only SMTP using Gmail or G-Suite SMTP Relay Server on Ubuntu Cloud. Also, change the Hostname and Display Full Name for emails from the server.

We can use Gmail SMTP for managing our outgoing emails and also receive in our inbox when properly configure Postfix program.

Generally, we will need a strong mailing system to communicate emails from our domain server. This internal emailing system helps us to receive notification about the server health as well as forward the incoming emails sent to the domain address.

In the following article, we will be installing and configure the Postfix program to SEND-Only using an SMTP server. Google Mail or Gmail supports the usage of their SMTP server settings on non-Google apps. We will be using the same in the below article to configure the postfix program.

Just in-case if you are following us, this is ourĀ fourth article and video demo under #CloudServer Series. We previously published about how to set up a powerful LEMP Stack on the Ubuntu cloud.

Let’s get started with configuring Postfix as SEND-Only SMTP on Ubuntu cloud —

What is Postfix?

Postfix is a free and open-source mail transfer agent(MTA) that routes and delivers electronic mail. It is released under the IBM Public License 1.0 which is a free software license.

It means you can download and distribute the software program freely without any problem. It’s one of the oldest and most used MTA software and also an alternative to the Sendmail program.

Postfix runs basically on the Unix system including our Ubuntu Cloud server. I personally use Postfix and recommend the same due to its simple configuration and setup.

Changing the Hostname on Ubuntu Server

Before we configure Postfix MTA, let’s adjust our hostname to reflect the correct domain name in our outgoing internal email.

Setting up a hostname is really important if you’re using a custom domain for emailing. The hostname helps in proper mailing address.

One of the easiest and reliable solutions that I recommend is using G Suite to have a business email address, for example: [email protected]. You can also get a 20% discount on sign up using this G Suite coupon code.

About Hostname, you can check your current hostname using the below command line in SSH:

hostname -f

The hostname command can also be used for changing the Ubuntu Server hostname throughout.

hostname example.com

I will be changing the hostname to demo.restorebin.com using below command.

hostname demo.restorebin.com

Changing Hostname from Terminal

But, this hostname will be valid only until restart. However, in order to make this change permanent edit, hostname and hosts file via SSH.

sudo nano /etc/hostname

Modify the hostname file to reflect below

demo.restorebin.com

Close the nano editor using Ctrl+c on the keyboard and save the changes. Next, modify the hosts files as well using nano editor:

sudo nano /etc/hosts

Modify line with IP Address 127.0.1.1 to reflect below:

127.0.1.1 demo.restorebin.com

Modified Hostname in /etc/hosts file

Close the nano editor, and reboot the Ubuntu server using below command:

reboot

Note: make sure that you’re using your domain name and address, the demo.restorebin.com is just for example.

That’s it! You have successfully changed the hostname of the cloud server.

Installing Postfix on Ubuntu Cloud

Get started with the install and configure Postfix by updating the apt repository first using below command:

sudo apt-get update

Run below command to install Postfix along with other mail utilities on the Ubuntu cloud.

sudo apt-get install mailutils

Update APT repository and Install Mailutils Program

During installation, you will be prompted to provide additional information and configure Postfix. First, it will ask to select the best suitable server settings. Since we are going to use SMTP, select the Internet option from the list and hit [Enter] button.
Configure Postfix Installation

Next, you will be asked to set the fully qualified domain name FQDN. This is nothing but the domain extension that you want to receive the email from.
Configure Postfix with FQDN hostname

We should generally set this same as hostname. Hence, fill in the same and hit on the [Enter] button. In my case, the hostname is:

demo.restorebin.com

You can always reconfigure the Postfix using below dpkg command:

dpkg-reconfigure postfix

Configure Postfix with Gmail SMTP

Now that your Postfix is installed, head over to configure Postfix to use Gmail for SMTP relay.

Open the Postfix configuration file main.cf using located in /etc/postfix/ directory.

sudo nano /etc/postfix/main.cf

Scroll to the bottom to find the relayhost = option and set it to Gmail SMTP server

relayhost = [smtp.gmail.com]:587

Postfix main.cf file updating relayhost

We will be using the encrypted TLS connection for all the outgoing emails, hence the port number is set to 587

Next, we will be adding a few lines at the end of all other existing code to enable secure authentication and read the hashed password for SMTP.

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

Also, make sure that myhostname = value is set to the hostname that we defined earlier. In my case, I’ll be setting it to:

myhostname = demo.restorebin.com

The last thing that you need to change in Postfix’s main configuration files is adjusting the mydestination value. Edit the value to reflect something similar to this.

mydestination = localhost.yourhostname.com, , localhost

main.cf file changes for postfix SMTP TLS authentication

You can now close the editor and save the changes.

Next, we will add the password credentials into sasl_password for authenticating the encrypted SMTP connection.

First, create a new file using nano editor:

sudo nano /etc/postfix/sasl/sasl_passwd

Add the Gmail password in below format in sasl_passwd file:

[smtp.gmail.com]:587 your-email-address@gmail.com:password

Replace with your Gmail address and password in the above code before pasting into sasl_passwd file.
Gmail SMTP Password om SASL Passwd file

Convert the sasl_passwd file into a database file, and delete the original file from the server. We can use the postmap command for converting.

sudo postmap /etc/postfix/sasl/sasl_passwd

This will create a sasl_passwd.db file in the same location.

Change the security and ownership of the password file to restrict root user access and read-write only.

chown root:root /etc/postfix/sasl/sasl_passwd
chmod 600 /etc/postfix/sasl/sasl_passwd

hash password and restrict authentication password to root

Finally, restart the postfix using the below command to make the changes permanent.

sudo service postfix restart

At last change the setting in Google Account to allow less secured non-Google apps to use authentication to send emails via SMTP on your behalf.
Enable Less Secure app access

Sending a Test Email

Once you’ve restarted the Postfix post-configuration, just try sending a test email using the below command.

echo "Test Postfix Gmail SMTP Relay via https://wpaq.com/?p=5809" | mail -s "Postfix Gmail SMTP Relay 1" your-email-address@gmail.com

Test Email received from Postfix SMTP

Do not forget to replace the Email ID with your personal mailing address.

Changing the Display Full Name

When you received your test email, you might have noticed that the display name is root.
Test Email received from root display name Postfix SMTP

Now if you want to change the display name, you need to define using chfn script. CHFN is nothing but an acronym for Change Full Name.

Let’s see what are names do the users have in our server using the below command.

getent passwd $USER | cut -d ':' -f 5 | cut -d ',' -f 1

This command will output all the User full name. And since we have only a root user, we will watch the only following output.

root

Using the below command you can change the display name or full name of any Unix user.

sudo chfn -f "FirstName LastName" username

For example, I will be changing the full name of root user to ‘restoreBin Demo’, hence my command line will be:

sudo chfn -f "restoreBin Demo" root

Change Full Name or Display Name in Ubuntu via terminal

That’s it, the Full Name or Display Name of the user has changed successfully.

Resend Test Mail

You’ve made the changes to Hostname as well as Display Name of the root Ubuntu user. Now try sending email again to check if the changes are applied using the same command.

echo "Test Postfix Gmail SMTP Relay post modification via https://wpaq.com/?p=5809" | mail -s "Postfix Gmail SMTP Relay 2" your-email-address@gmail.com

Full Name or Display change in Postfix mail

This will also help to validate whether the server is intact and not broken after changes.

Video on Postfix Configuration

Watch and learn to configure Postfix on Ubuntu Cloud Server with Gmail SMTP Server for internal emails.

Hope you liked the video, please subscribe to our channel.

What’s next in the #CloudServer series?

In this article, we learn about the emailing system and configure Postfix on Cloud Server. In the next article, I will be sharing on how to generate a free SSL certificate using LetsEncrypt.

Let’s Encrypt is a free Open Source certificate generator powered by the community which helps in establishing the secured connection between host and server. You might also want to know that HTTPS secured connection is also a ranking factor in search engine optimization.

If you've any thoughts on Install and Configure Postfix with Gmail SMTP for Perfect Mailing System, then feel free to drop in below comment box. Cheers!

Share
Kushal Azza
Kushal Azza
Fond of latest technologies and gadgets. I'm a digital dev and analytics consultant. Also, the geek behind this blog!

2 Responses

  1. Ravindra says:

    Thanks for the video. As per your article I have configured a postfix server and able to send mails. I am using gsuite as mail server. Please suggest me two things:
    1. I want to send the messages to a list of recipients. Till now I was using phpmailer but it is very slow so I want to use postfix for sending messages. How I can do that?
    2. Is it possible to use the phpmailer with postfix. I searched for that but could not find.
    Thank You

    • Kushal Azza says:

      Hi Ravindra, thanks for your comment.
      I’m really sure about the phpmailer (still learning though).
      I feel that you might be aware of this. Be cautious about is Gmail SMTP Relay sending limit. If you’re overusing the limit or sending bulk emails may lead to suspension and also categorized under spam.

Leave a Reply

Your email address will not be published. Required fields are marked *