Skip to main content
Menu
Configure SMTP with Gmail Using Postfix | Cheatsheets
Configure SMTP with Gmail Using Postfix

Configure SMTP with Gmail Using Postfix

September 3, 2021
linux, mail
devops, monitoring

Setup Google App password #

  1. Go account security https://myaccount.google.com/security
  2. Create App Password
    • Select App as Mail
    • Select Device (other) > name your system
    • Generate
  3. Note down Generated password

img

Install all necessary packages #

sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules

Edit postfix config file #

vi /etc/postfix/main.cf

and following lines to it:

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

Edit sasl_passwd file #

vi /etc/postfix/sasl_passwd

add gmail username and generate password

[smtp.gmail.com]:587    USERNAME@gmail.com:GENERATED-PASSWORD

Fix permission and update postfix config to use sasl_passwd file #

sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

Reload postfix config for changes to take effect #

sudo /etc/init.d/postfix reload

Testing #

echo "Test mail from postfix" | mail -s "Test Postfix" you@example.com