There are hundreds of blog posts explaining how to set up email on your Linux machine and the relay portion is a real problem. Most of those will only work on one version of one flavor of Linux and they will FLAKE OUT on any other system. The first few times I tried to set this up, I realized what a circus it is. I ran across a brilliant set of instructions and now I am eager to share!
This post finally got me on the right track: https://linuxconfig.org/configuring-gmail-as-sendmail-email-relay
The author clearly wrote it for Ubuntu but it was so easy to adapt to CentOS 8. Other methods worked on Ubuntu but didn’t work on CentOS 7, or worked on CentOS 7 but nothing else. All in all, CentOS 8 has been the biggest challenge.
This is how I implemented on my CentOS 8 machine:
Logged into Google with my “From” gmail account (not my main email address, this is one I just set up for email alerts). Click my user profile icon at the top right–>Manage your Google Account–>Security. 2-factor authentication should be set to ON. Create an app password and make a note of it.
#Sendmail install
sudo yum install sendmail -y
sudo yum install sendmail-cf -y
sudo yum install mailx -y
#Create Gmail Authentication file
sudo -s
mkdir -m 700 /etc/mail/authinfo
cd /etc/mail/authinfo
touch gmailauth
nano gmailauth
#this is the configuration to put in the gmailauth file
AuthInfo: “U:root” “I:LogAlertEmailAddress@gmail.com” “P:Your16DigitAppPassword”
#Hashmap the gmailauth file
makemap hash gmailauth < gmailauth
#Configure the sendmail.mc file
cd /etc/mail
nano sendmail.mc
#This is the configuration to insert before the MAILER definition line in sendmail.mc
define(`SMART_HOST’,`[smtp.gmail.com]’)dnl
define(`RELAY_MAILER_ARGS’, `TCP $h 587′)dnl
define(`ESMTP_MAILER_ARGS’, `TCP $h 587′)dnl
define(`confAUTH_OPTIONS’, `A p’)dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN’)dnl
define(`confAUTH_MECHANISMS’, `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN’)dnl
FEATURE(`authinfo’,`hash -o /etc/mail/authinfo/gmailauth.db’)dnl
#Rebuild Sendmail Configuration and restart the service
make -C /etc/mail
systemctl restart sendmail
#Test the mail service from the command line
echo “Test Message 1” | mail -s “Sendmail Test” TargetAddress@gmail.com
