Postfix is an open-source Mail Transfer Agent (MTA) that acts as an 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 reasons 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:

Terminal command to update the system before installing Postfix

  • 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 the 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 Postfix and libsasl2, a package that helps to manage Simple Authentication and Security Layer (SASL).

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

Terminal command to install Postfix and the libsasl2-modules package

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

Postfix configuration prompt with Internet Site selected

  • Enter the fully qualified name of your specific domain.

Postfix prompt to enter the fully qualified domain name

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

Terminal command to check the myhostname parameter in Postfix

II. Create an App Password for Postfix

When you enable 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. This is a very important security measure that is developed to restrict any unauthorized user from accessing the account, but it also halts sending email through SMTP clients. Here are the following steps to configure the Gmail account to create the Postfix-specific password:

  • Sign in to your Gmail account, 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. Ensure that Two-Step Verification is enabled.
  • Now, Generate the App password for Postfix:

Google account page to generate an App password for Postfix

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

Newly generated Google App password with Done button

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 the SMTP host, username, and password details:

sasl_passwd file with the Gmail SMTP host, username and password

Note: Message submission can be done over port 587 (StartTLS) or port 465 (SSL). 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:

Terminal postmap command to generate the hash DB file

If everything goes perfectly, 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 plain text.

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

Terminal commands to change ownership and permissions of the password files

V. Configure Postfix SMTP Relay Server

In this segment, you can configure the /etc/postfix/main.cf file to use 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.

Postfix main.cf file with the Gmail relay host setting

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

Postfix main.cf parameters to enable authentication

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

Terminal command to restart 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”, 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 the help of a reliable third-party tool, i.e., SysTools MBOX to G Suite Import Tool. It is designed in such a way as 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 the 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 SysTools MBOX to G Suite Import Tool, which helps to export multiple .mbox files to Gmail at once without any hassle.