Configuring Postfix notification using Gmail SMTP server.
Mahinsha Nazeer

Mahinsha Nazeer @mahinshanazeer

About: Certified engineer—just one loose nut away from a total breakdown

Location:
Hyderabad, India
Joined:
Apr 14, 2025

Configuring Postfix notification using Gmail SMTP server.

Publish Date: Apr 20
0 0

In this blog, we will look into how to install and set up the Postfix email server software on a Linux system to enable you to send messages within your network. We aim to set up a postfix in a dynamic network for sending mail. Let's dive deep into how to configure Postfix as an SMTP server and configure a Postfix to use any other SMTP services (ex: Gmail, Zoho) for easy setup. In this session, we will be using Gmail SMTP server.

Postfix is a Mail Transfer Agent (MTA) server that was developed as a replacement for sendmail, which is the default MTA server on many older Linux systems. Because of its modular pipeline-based architecture, Postfix is versatile and integrates easily with many other services, such as spam and anti-virus processing, as well as with message store software, such as the Dovecot IMAP and POP server.

Installing postfix in Ubuntu:

sudo apt install postfix -y
sudo apt install mailutils -y
sudo systemctl enable postfix
Enter fullscreen mode Exit fullscreen mode

If you are using any firewalls don’t forget to allow the ports 25 and 587 in the firewall.

Now we need to go to postfix configuration, First take a backup of the existing configuration:

sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.bak
Enter fullscreen mode Exit fullscreen mode

Now you can start editing the main.cf file, the following is my current template, here I am using Gmail as my SMTP server:

myhostname = worker2.local
mydomain = localhost
myorigin = /etc/mailname
inet_interfaces = all
mydestination = $myhostname, $mydomain, localhost.localdomain, localhost
sender_canonical_maps = hash:/etc/postfix/sender_canonical
smtp_generic_maps = hash:/etc/postfix/smtp_generic

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
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Enter fullscreen mode Exit fullscreen mode

Here I am using a dynamic IP address and DDNS at home. As for now, we are using Google SMTP service to send mail from our server.

Go to the following URL and create an app password for mail:

Google-App

Once you are logged in to your Google account, follow the below steps:

  1. Go to the above URL and log into the Google account.

In the above window, give any name for the app. You can see the one I have already created with the name ‘Mail’ and click next. You will get a 12-digit passcode copy and paste the passcode without removing the spaces between them.

Now in the Linux server, edit the ‘sasl_passwd’ file. If the file doesn’t exist, create one.

sudo vi /etc/postfix/sasl_passwd
Enter fullscreen mode Exit fullscreen mode

The following should be the file content:

[smtp.gmail.com]:587 mymail@gmail.com: ****TYYN SY**  ****
Enter fullscreen mode Exit fullscreen mode

Now try to restart Postfix and send a mail using the following commands:

sudo systemctl restart postfix
echo "mail body" | mailx -s "subject" test@example.com
Enter fullscreen mode Exit fullscreen mode

In order to trouble shoot any issues with postfix, you can refer following logs;

/var/log/maillog
/var/log/mail.log
/var/log/syslog
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment