A Comprehensive Guide to Connect Postfix to Gmail Account

Written By Siddharth Tehri
Anuraag Singh
Approved By Anuraag Singh
Published On June 16th, 2022
Reading Time 6 Minutes Reading

Postfix is an open-source Mail Transfer Agent (MTA) that acts as SMTP server or client for sending or receiving emails. There are so many reasons due to which users need to configure Postfix to send mail using Gmail and Google Apps. One of the main reason is to avoid getting your email flagged as spam if the current server IP has been added to the blacklist. Therefore, in this blog, we are going to discuss a step-by-step guide on how to connect Postfix to Gmail account in Ubuntu to send email through Gmail and Google Apps.

Before Getting Started

  • Although not necessary, it is advised to update your system:

  • Use your browser to check your email login credentials by logging into your Gmail account. This is required to check if the email account to be configured is a valid one.

Sequential Steps to Connect Postfix to Gmail Account

The process starts with installing of required packages to finally configuring the account in Postfix. Follow the guide in the below section for detailed instructions.

I. Install Postfix Mail Server

Now, you can install the Postfix or libsasl2, a package that helps to manage Simple Authentication and Security Layer (SASL).

  • You can install Postfix and libsasl2-modules package too:

  • During the installation of Postfix, a prompt will pop up asking for the General type of mail configuration. Then, choose Internet Site:

  • Enter the totally qualified name of your specific domain.

  • After completion of an installation process, confirm that the myhostname parameter is already configured with the server’s FQDN:

II. Create an App Password for Postfix

When you enable the two-Factor Authentication (2FA), Gmail is pre-configured to decline connections from different applications like Postfix which does not support the second step of authentication. Otherwise, this is a very important security measure that is developed to restrict any unauthorized user from accessing the account, it halts sending email through SMTP clients. Here are following steps to configure Gmail account to create the Postfix-specific password:

  • Sign in to your email address, then you will need to Manage your account access and security settings. Move towards “Password & sign-in method” and click the 2-Step Verification. You might be asked for the password and verification code before continuing further. You may ensure that Two-Step Verification is enabled.
  • Now, Generate the App password for Postfix:

  • Go to Select app and, then choose Other (custom name) from drop-down menu. Enter the Postfix and click Generate.
  • Now, you can see the newly generated password. Write it down or keep it somewhere secure that you will be able to find easily in the further steps, then press Done:

III. Add Gmail account Username & Password to Postfix

Username and password are saved in sasl_passwd inside the /etc/postfix/sasl/ directory. So, you will need to add your mail login credentials to this particular file and to Postfix. By doing this we will get one step closer to eventually connecting Postfix to Gmail account.

  • You can open or create /etc/postfix/sasl/sasl_passwd file and then add SMTP Host, username, and password detail:

Note: Message submission can be done over 587(StartTLS) or 465(SSL) port. Whichever you choose, make sure to use the same port number throughout the procedure.

  • Generate the hash DB file for Postfix by executing the postmap command:

If everything is going perfect, you will receive a new file named sasl_passwd.db in this /etc/postfix/sasl/ directory.

IV. Keep Postfix Hash Database and Email Password File Safe

These files /etc/postfix/sasl/sasl_passwd and /etc/postfix/sasl/sasl_passwd.db are created in the above steps that contain your SMTP credentials in a plain text.

Restricting access to files, change all the permissions so that only a root user can read from or write to specific files. Run the following cmdlets to change an ownership to root and update file permissions for two files:

V. Configure Postfix SMTP Relay Server

In this segment, you can configure the /etc/postfix/main.cf file to utilize the Gmail’s SMTP server.

  • Search and modify relayhost in /etc/postfix/main.cf to match the example given below. Make sure that the port number suits what you defined in /etc/postfix/sasl/sasl\\_passwd above.

  • At the end of a file, add the list of following parameters to enable authentication:

  • Save all the changes and close that file.
  • Finally, you can restart the Postfix:

User’s Problem

“I was unable to find any documentation on postfix stored emails. I don’t even know where these emails are stored and what is the file format? I need to move these emails from Postfix to Gmail. Currently, I am using Ubuntu Server 11. Can anyone suggest me a feasible solution for the same? Any help or suggestion would be highly appreciated!!”

The standard format, called “MBOX” that starts with “From” to specify the start of every message – this is the main reason why many email programs will change “From” in the body of the email message to “>From”. Also, you can configure it by using “Maildir”, in which /var/mail/[username] is a directory where each message is a certain file in that directory.

Alternate Solution to Migrate Postfix to Gmail Account

If the above-stated method does not work well, users can take help of a reliable third-party tool, i.e., MBOX to G Suite Migrator. It is designed in such a way to perform bulk conversion from MBOX to Google Apps without any data loss. The interface is simple and easy-to-use. It also helps to retain the on-disk folder hierarchy of MBOX after the conversion process. It is compatible with all latest versions of Windows operating system including 32-bit and 64 bit.

Final Words

Many users search for a reliable and efficient solution to migrate Postfix to Gmail. Therefore, in the above section, we have described a step-by-step procedure to move MBOX file to Google Apps. But, sometimes users may face various problems when performing the manual solution. Thus, we have suggested a trustworthy approach named as MBOX to G suite Migrator, which helps to export multiple .mbox files to Gmail at once without any hassle.


By Siddharth Tehri

Writing technical blogs and articles to help users to resolve technical challenges. Having expertise in various technical arenas. Working in an IT organization as a Senior Technical Analyst. Loves to discover new technical challenges and provide easy solutions to resolve them.