How to Relay Emails to Gmail

From DISI
Revision as of 22:43, 1 September 2022 by Jgutierrez6 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

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

Two machines that send emails are:

  • vav.combpio.ucsf.edu
  • bksmailman.compbio.ucsf.edu

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

Instructions

Reference Tutorial

  1. Create a gmail account OR Ask Khanh for docking.email@gmail.com 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/main.cf
      
      //Copy these lines and paste it at the bottom of the main.cf file
      relayhost = [smtp.gmail.com]: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
      [smtp.gmail.com]:587 docking.email@gmail.com:<gmail_app_password>
  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.