postfix


How to Use Gmail to Send Email with Postfix on Ubuntu

Introduction

In today’s digital age, email has become integral to our personal and professional lives. While numerous email clients are available, Gmail remains a popular choice due to its user-friendly interface and powerful features. In this blog post, we will guide you through configuring Postfix, a famous mail transfer agent (MTA), to send emails using your Gmail account on an Ubuntu system. Let’s dive in!

Prerequisites:

Before we begin, make sure you have the following prerequisites in place:

  1. An Ubuntu system with administrative privileges.
  2. A Gmail account.
  3. A basic understanding of the Linux command line.

Step 1: Install Postfix

  1. Open the terminal on your Ubuntu system.
  2. Update the package lists by running the command:
sudo apt update;

Install Postfix by running the command:

sudo apt install postfix;

During the installation process, you will be prompted to configure Postfix. Select “Internet Site” and press Enter. Provide your system’s mail name when prompted.

Step 2: Configure Postfix to Use Gmail

Open the main Postfix configuration file using a text editor:

sudo nano /etc/postfix/main.cf;

Locate the following lines and modify them as shown below:

relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous

Save the changes and exit the text editor.

Step 3: Configure Gmail Account Credentials

Create a file to store your Gmail account credentials:

sudo nano /etc/postfix/sasl_passwd;

Add the following line to the file:

[smtp.gmail.com]:587 [email protected]:your_app_password

Note: Replace “[email protected]” with your actual Gmail address and “your_app_password” with the application-specific password you generated for Postfix in your Google Account settings. Save the file and exit the text editor. Secure the credentials file by running the command:

sudo chmod 600 /etc/postfix/sasl_passwd;

Update the Postfix lookup table for the credentials file by running the command:

sudo postmap /etc/postfix/sasl_passwd;

Step 4: Restart Postfix and Test

Restart the Postfix service to apply the changes:

sudo systemctl restart postfix;

Test the email configuration by sending a test email using the following command:

echo "This is a test email." | mail -s "Test Email" [email protected];

Note: Replace “[email protected]” with the email address where you want to send the test email.

Check the recipient’s mailbox to ensure the test email was delivered successfully.

Conclusion

By configuring Postfix to use Gmail on your Ubuntu system, you can harness the power of both platforms to send emails seamlessly. This integration allows you to leverage Gmail’s advanced features while benefiting from Postfix’s reliability and customization options. With the steps outlined in this blog post, you can easily set up the connection and streamline your email communication. Happy emailing!


Host or domain name not found. Name service error for name=smtp.gmail.com type=AAAA: Host not found, try again

Recently, a postfix mail server running on Ubuntu gave us the following error while trying to send an email to Bob:

Jun 16 17:02:03 gateway postfix/smtp[23522]: ED3799A0D27: to=[email protected], relay=none, delay=0.02, delays=0.01/0.01/0/0, dsn=4.4.3, status=deferred (Host or domain name not found. Name service error for name=smtp.gmail.com type=AAAA: Host not found, try again)

After several attempts like changing the protocol (inet_protocols) to IPv4 (since AAAA is an IPv6 A-record) in /etc/postfix/main.cf and so on we could not get it running.. We restored the configuration file to its original and then we restarted the service:

sudo service postfix restart;

It worked!…. no idea why..