Configure Postfix to Use Gmail SMTP on Ubuntu


Introduction

Postfix is open-source mail transfer agent (MTA) it allows users to send and receive email on Linux system. It is efficient and easy-to-use solution, it can configured to work with various email providers, including Gmail.

In this article, we will show you steps to configure Postfix using Gmail SMTP on Ubuntu. This process involve setting up Gmail account to allow any apps to access it, installing and configuring Postfix, testing setup to ensure it works.

Before dive into details, let's take why you might want to use Gmail SMTP with Postfix.

Why Use Gmail SMTP with Postfix?

Using Gmail SMTP with Postfix offers several benefits, including −

  • Reliability − Gmail has a robust infrastructure that ensures your emails are delivered without any issues.

  • Security − Gmail SMTP uses encrypted connections to protect your email data from interception by third parties.

  • Spam Protection − Gmail has advanced spam filters that help prevent your emails from being marked as spam.

  • Cost-effective − Gmail provides free SMTP service up to a certain limit, making it a cost-effective option for small businesses and individuals.

Now that we understand benefits of using Gmail SMTP with Postfix, let's proceed to configuration steps.

Step 1: Allow External Access to Gmail

Before you can use Gmail SMTP with Postfix, you need to allow external access to your Gmail account. This involves enabling "Less Secure Apps" feature on your Google account settings.

To enable this feature, follow these steps −

  • Go to Google Account Security page by clicking on this link −

    https://myaccount.google.com/security

  • Click on "Less secure app access" section.

  • Toggle on "Allow less secure apps" switch.

Note − Enabling this feature may increase risk of unauthorized access to your Google account. However, it is necessary for Postfix to access your Gmail account.

Step 2: Install Postfix

After enabling "Less Secure Apps" on your Gmail account, next step is to install Postfix on your Ubuntu system. You can do this by running following command −

sudo apt-get install postfix

The command will prompt you to select mail server configuration type. Choose "Internet Site" and click "Ok." This will launch a configuration wizard that will guide you through setup process.

Step 3: Configure Postfix

Once you have installed Postfix, next step is to configure it to work with Gmail SMTP. This involves editing main configuration file located at /etc/postfix/main.cf.

To edit the file, open it in your preferred text editor using following command −

sudo nano /etc/postfix/main.cf

Then add following lines at bottom of the file −

SMTP Settings for Gmail

relayhost = [smtp.gmail.com]:587 
smtp_sasl_auth_enable = yes 
smtp_sasl_security_options = noanonymous 
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd 
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt 
smtp_use_tls = yes

The above settings configure Postfix to use Gmail SMTP as relay host, enable authentication, and use SSL/TLS encryption.

Step 4: Create a Password File for Authentication

To authenticate your Gmail account with Postfix, you need to create a password file. This file will contain your Gmail email address and password.

To create password file, run following command −

sudo nano /etc/postfix/sasl_passwd

Then add following line to the file −

[smtp.gmail.com]:587 
your_email_address@gmail.com:your_gmail_password

Replace "your_email_address" with your Gmail email address and "your_gmail_password” with your actual Gmail password.

After adding line, save and exit the file.

Next, run following command to create a hash map of password file −

sudo postmap /etc/postfix/sasl_passwd

This command will create a new file called sasl_passwd.db in same directory as sasl_passwd file.

Step 5: Restart Postfix

After configuring Postfix and creating password file, you need to restart service for changes to take effect.

To restart Postfix, run following command −

sudo service postfix restart

Step 6: Test the Configuration

To test Postfix configuration, you can send a test email from command line using following command −

echo "This is a test email." | mail -s "Test email" recipient_email_address

Replace "recipient_email_address" with email address of recipient.

If configuration is correct, you should receive the email in your recipient email address.

Here are some additional details to consider when configuring Postfix to use Gmail SMTP on Ubuntu −

Using an App Password

If you have two-factor authentication (2FA) enabled on your Gmail account, you may need to use an app password instead of your regular Gmail password. To create an app password, follow these steps −

  • Go to Google Account Security page.

  • Click on "App passwords" section.

  • Select "Mail" and "Other (Custom name)" from dropdown menu.

  • Enter a custom name for app password and click "Generate."

  • Copy generated password and use it in place of your regular Gmail password in sasl_passwd file.

Troubleshooting Common Issues

If you encounter any issues while configuring Postfix to use Gmail SMTP, here are some common solutions −

  • If you get a "SASL authentication failed" error, check your sasl_passwd file and make sure it contains correct email address and password. Also, ensure that sasl_passwd file has correct permissions (600).

  • If you get a "Connection timed out" error, check your firewall settings and ensure that port 587 (Gmail SMTP port) is open.

  • If you get a "Certificate verification failed" error, ensure that ca-certificates package is installed on your system. You can install it using following command −

sudo apt-get install ca-certificates
  • If you get a "Message size exceeds maximum allowed" error, check your Postfix configuration and increase message_size_limit parameter in the main.cf file.

Using Other Email Providers

While this article focuses on using Gmail SMTP with Postfix, you can use other email providers as well. The configuration steps may differ depending on provider, but general principles remain same.

Some popular email providers that work well with Postfix include Yahoo, Microsoft, and Amazon SES.

Advanced Configuration

If you want to customize your Postfix configuration further, you can explore various settings available in the main.cf file. These include options for email filtering, domain name resolution, SMTP relay access, and more.

However, be careful when modifying these settings, as incorrect configurations can cause issues with your email delivery. It is recommended to consult Postfix documentation or seek help from a qualified professional when making advanced configurations.

Conclusion

In conclusion, configuring Postfix to use Gmail SMTP on Ubuntu is a simple and effective way to send and receive email on your Linux system. By following steps outlined in this article, you can easily set up and test your Postfix configuration to ensure it works correctly.

Remember to keep your Gmail account secure by disabling "Less Secure Apps" feature once you are done testing. Also, regularly update your system and use strong passwords to protect your email data from unauthorized access.

Updated on: 03-Mar-2023

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements