How to use postfix to relay outgoing emails through a Gmail account.

There are numerous cases in which some businesses want to use a free Gmail account for outgoing emails.  That can vary from informational emails created by an application, or email alerts of a monitoring system that goes to your internal team and wants to get a paid license of your existing domain.  I will explain in this step-by-step guide how to use Postfix to relay outgoing emails through a Gmail account.

Postfix-Mouse-Gmail

Prerequisites:

  • A Gmail account with 2-step verification.
  • An App Password generated for the Gmail account.
  • A Linux system (in our case we will use Ubuntu Server).
  • The application you want to relay from should be on the same server. I will create a future article on how to configure postfix to accept connections from other servers too.
  • Some patience even the procedure is very straightforward 😊.

Prepare Gmail Account:

Go to the Gmail account that you want to use for this purpose and click on Account ->  Manage your Google Account.

Manage-Your-Google-Account

Go to Security and Enable 2-Step Verification with authentication method you prefer.

Security-2-Step-Verification

Then go to search and type “App passwords”.

App-Passwords
App-Passwords-Creation

Choose a name and create an App password for the postfix authentication. Keep the password safe as we will need it later.

Generated-App-Password

Postfix Installation:

During installation the wizard might appear. Select “Internet Site.” Also set the system mail name (this doesn’t affect the relaying so you can pick your preferred domain here).

Postfix-Configuration

Ensure Postfix is running:

Configure Postfix:

Open main configuration file.

Modify or add the following lines to configure Gmail as the relay host. Some of the following lines already exist in the configuration, so if you intend to copy-paste the code, below make sure you find and comment out or delete those before you save the file.

Setup Authentication:

Create the /etc/postfix/sasl_passwd file to store Gmail credentials.

Add the following line, replacing your-email@gmail.com and your-app-password with your Gmail credentials.

Secure the file.

Hash the file to create a database for Postfix.

Restart Postfix.

Test the configuration:

Install the Mailutils package to send a test email directly from shell.

Send a test email.

Check the Postfix logs for any issues.

Conclusion:

By following this step-by-step guide in how to use Postfix to relay outgoing emails through a Gmail account, you will have all the benefits of using a fully compliant mailbox and you won’t have to use the internal Sendmail account which needs a serious level of configuration in order to support all the modern authentication mechanisms that email servers using (DKIM, DMARC etc.). Here we have a list with the best open-source IT monitoring tools for 2024. If you are looking such tool here you can learn more .

If the above sounds confusing, very technical, or just time consuming for you current capacity, we are here to help Contact us by Clicking Here. We are helping businesses to stabilizing and optimizing their environments. We also offer monitoring as a service if you just want us to keep an eye and alert you if something has indications of the will stop working soon. Here is an article related to monitoring

About Alexios Pappas – Alexios Pappas has worked internally in IT Departments in multi-role technical and management positions for many years. In parallel, Alexios led IT multinational projects across several countries globally as an IT Freelancer. He repeatedly got Top Rated Plus distinction and also 100% satisfaction rate for over one and a half-decade, resulting from the Client’s feedback. Since 2015 he has been focused on informing and helping Businesses that want to improve and excel in their IT Section.