Configure Postfix to use Gmail as a Mail Relay on UBUNTU Server.

Configure Postfix to use Gmail as a Mail Relay(UBUNTU Server)

If you have a Gmail account, you can configure your Material Transfer Agent (MTA) to relay outgoing mail through Gmail.

This gives you the benefit of Gmail’s reliability and robust infrastructure, and provides you with a simple means of sending email from the command line.

In this Article, you will learn how to install and configure a Postfix server on Debian or Ubuntu to send email through Gmail and Google Apps. For information on configuring Postfix with other external SMTP servers.

The main advantage of postfix is

It is a  free, open-source, actively maintained, and highly secure mail transfer agent.

–> At each step, we’ll cover instructions specific to the following operating systems:

Before You Begin :

1.Server should ping with

2.Server should have host name(Public IP)

3.Server should be updated.

Step :1 

Connect to server and check hostname and server is pinging with internet or not.

Step 2 : 

we need to update the server by following command called “apt-get update”

Update your system and  also install postfix mailutils.

The command we need to follow.

Related Screenshot :

During the Postfix installation, a prompt will appear asking for your General type of mail configuration. Select Internet Site:

Enter the fully qualified name of your domain. In this example,

When prompted for a “Mail name,” choose a hostname to be used in mail headers as the origin of your emails. A fully-qualified domain name is preferred, but using your machine’s simple hostname is OK. Regardless of what you enter here, your return address will appear to recipients as your Gmail address.

Once hostname is set installation of postfix will complete and automatically the inputs of postfix will be written to  /etc/postfix/



Step 4 :

Configure Gmail Authentication

Create or modify a password file which will be used by Postfix to establish authentication with Gmail. In the authentication information below, replace username with your Gmail username and password with your Gmail password. If you are using a custom Gmail Apps domain name, you may replace with your Google Apps domain.

The password file will reside in the Postfix configuration directory. The file can be named whatever you like, but the recommended filename is sasl_passwd.

Postfix configuration files reside in the directory /etc/postfix. Create or edit the password file:

Add the line :

Here we need to specify gmail username and password.

Save and close the file. Your Gmail password is stored as plaintext, so make the file accessible only by root:

Step 5 :

Configure Postfix

There are six parameters which must be set in the Postfix configuration file The parameters are:

relayhost, which specifies the mail relay host and port number. The host name will be enclosed in brackets to specify that no MX lookup is required.

smtp_use_tls, which enables (or disables) transport layer security.

smtp_sasl_auth_enable, which enables (or disables) SASL authentication.

smtp_sasl_security_options, which in the following configuration will be set to empty, to ensure that no Gmail-incompatible security options are used.

smtp_sasl_password_maps, which specifies the password file to use. This file will be compiled and hashed by postmap in a later step.

smtp_tls_CAfile, which specifies the list of certificate authorities to use when verifying server identity.


Edit the main Postfix configuration file:

Add or modify the following values:

Step 6 :

Process Password File

Use postmap to compile and hash the contents of sasl_passwd. The results will be stored in your Postfix configuration directory in the file sasl_passwd.db.

Step 7 :

Restart Postfix

Restart the Postfix service, putting your changes into effect.

Step 8 :

Enable “Less Secure Apps” In Gmail

By default, only the most secure sign-ins, such as logging in to Gmail on the web, are allowed for your Gmail account. To permit relay requests, log in to your Gmail account and turn on Allow less secure apps.

For more information, review the Google Support document “Allowing less secure apps to access your account.”

Step 9 :

Send A Test Email

Test your new configuration by sending an email using the mail command. Run:

mail -s “Test subject”

You will be presented with a blank line (or a CC: field, which you can bypass by pressing Enter). Type the body of your message, pressing Enter for new lines. When you are finished composing the email, type CTRL-D to send it. To cancel the email, press CTRL-C twice.

Step 10 :


Thank you ……….

If any suggestions please write in comment section.

Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 5.00 out of 5)

One thought on “Configure Postfix to use Gmail as a Mail Relay on UBUNTU Server.

Add Comment