How to Relay Emails to Gmail

Jump to navigation Jump to search


This tutorial applies to RHEL 8 based operating systems (CentOS, Rocky Linux)

Two machines that send emails are:


Relaying emails through gmail will make it more secure and trustworthy.


Reference Tutorial

  1. Create a gmail account OR Ask Khanh for for credentials.
  2. Disable pre-installed 'sendmail' package
    • rpm -q sendmail (Check for package)
      systemctl disbale sendmail --now (Disable package)
  3. Enable smtp in firewall
    • firewall-cmd --permanent --add-service=smtp
  4. Install Postfix and other dependencies
    • yum install postfix mailx cyrus-sasl cyrus-sasl-plain -y
  5. Configure Postfix
    • vim /etc/postfix/
      //Copy these lines and paste it at the bottom of the file
      relayhost = []:587
      smtp_use_tls = yes
      smtp_sasl_auth_enable = yes
      smtp_sasl_security_options = noanonymous
      smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
      smtp_tls_security_level = encrypt
      smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
  6. Set up authentication file for gmail
    • vim /etc/postfix/sasl_passwd
      //Paste this line and replace
  7. Create Gmail App Password For Authentication
    • Log in to gmail account
    • Click top right account button
    • Click 'Manage Google Account'
    • On the left, click the 'Security' tab
    • Enable '2-step Verification'
    • Now back to security tab and click 'App Passwords'
    • Generate App Password
    • Click 'Other' for the category input
    • Copy the generated token and save somewhere
  8. Once you have token, replace <gmail_app_password> in sasl_passwd file with the token and save the file
  9. Update the password database
    • postmap /etc/postfix/sasl_passwd
      chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
      chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
      systemctl restart postfix.service
      systemctl status postfix
  10. Verify that it works
    • echo "This is the body of the email" | mail -s "This is the subject line" <your_email>
  11. If it works you're done. If it doesn't check the reference tutorial for troubleshooting.