Sendmail via Gmail SMTP relay

1.9k views Asked by At

I'm running a CentOS 7 VM in Google Cloud but having difficulty sending emails out using sendmail. I've followed the below posts and numerous others but the result remains the same, i.e. the mails never reach their destination.

https://linuxconfig.org/configuring-gmail-as-sendmail-email-relay

https://www.bonusbits.com/wiki/HowTo:Configure_SendMail_to_Use_SMTP_Relay

https://tecadmin.net/sendmail-to-relay-emails-through-gmail-stmp/

http://ibgwww.colorado.edu/~lessem/psyc5112/usail/mail/debugging/

I've used a telnet session to connect to smtp-relay.gmail.com to test it from the server and was able to connect and send a mail, which also reached it's destination.

Please forgive my ignorance, I'm very new to Linux. When I use the mail -v command to send the mail I get the output below.

[root@backend-main-test mail]# echo "Just testing gmail relay" | mail -v -s "Sendmail gmail relay" [email protected]

[email protected]... Connecting to [127.0.0.1] via relay...
220 backend-main-test.c.somecompany.internal ESMTP Sendmail 8.14.7/8.14.7; Sun, 3 Jun 2018 17:59:57 GMT
>>> EHLO backend-main-test.c.somecompany.internal
250-backend-main-test.c.somecompany.internal Hello localhost [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE
250-DSN
250-ETRN
250-DELIVERBY
250 HELP
>>> MAIL From:<[email protected]> SIZE=252
250 2.1.0 <[email protected]>... Sender ok
>>> RCPT To:<[email protected]>
>>> DATA
250 2.1.5 <[email protected]>... Recipient ok
354 Enter mail, end with "." on a line by itself
>>> .
250 2.0.0 w53Hxvqg002267 Message accepted for delivery
[email protected]... Sent (w53Hxvqg002267 Message accepted for delivery)
Closing connection to [127.0.0.1]
>>> QUIT
221 2.0.0 backend-main-test.c.silveraxiom-prod.internal closing connection
[root@backend-main-test mail]#

It gives the message "Message accepted for delivery" at the end, but at the beginning it says connecting to 127.0.0.1 (localhost), should it not instead be connecting to the smtp-relay.gmail.com host I specified in the sendmail.mc and auth / authinfo files' configuration?

According to the /var/log/maillog when I execute those command it's giving the "Message accepted for delivery" like above and the relay is specified as 127.0.0.1

2

There are 2 answers

0
AnFi On

Sendmail relays messages via 127.0.0.1:25 to local sendmail daemon to avoid being set root id.
[Set root id sendmail had created long strem of security problems.]

To test your sendmail daemon configuration send test message as root in verbose mode with map lookups tracking (authinfo lookups).

#!/bin/sh
# -i - no special treatment of lines starting with dot
# -v - verbose mode, also turns on SMTP trascript
# -d60.5 - trackimg map lookups including authinfo lookups
# -Am - use sendmail.cf configuration file instead of submit.cf
/usr/sbin/sendmail -i -v -Am -d60.5 -- [email protected] <<END
Subject: gmail test

gmail test
END
0
Steven Tuminelli On

I created a sendmail via Gmail script for ubuntu and centos.. you are free to use it if you like.

#! /bin/bash

date=$(date +"%Y-%m-%d")
logFile=:Log File Location Goes HERE
authInfoPath="/etc/mail/authinfo/"
idpass="/etc/mail/authinfo/gmail-idpass"
sendmail="/etc/mail/sendmail.mc"

## Functions

determineLinuxFlavor()
{
    os=$(grep -i "NAME=\"Amazon\ Linux\ AMI\"" /etc/os-release)

    if [ -z "$os" ]; then
        os=$(grep -i "NAME=\"Ubuntu\"" /etc/os-release)

        if [ -z "$os" ]; then 
            os="UNKNOWN"
        else
            os="UBUNTU"
        fi
    else
        os="CENTOS"
    fi

    printf $os
}

os=$(determineLinuxFlavor)

## About to start configuring send mail to relay through Gmail. ##

## @TODO:  get the OS version and install dependencies based on OS

if [ $# -eq 5 ]; then
    email=$1
    password=$2
    response=$3
    choice=$4
    personal=$5
else
    # ask questions here
    echo "## Enter the credentials of Gmail User account you wish to use. ##"
    read -r -p "Enter the username of the Gmail account you are adding:  " email
    read -r -p "Enter the password of the Gmail account you are adding:  " password
    read -r -p "Would you like to send a test email? [y/N] " response
    read -e -p "Would you like to check the log tail for errors? [y/n] " choice
    read -e -p "Enter a personal email address to test the relay instalation:  " personal
fi

## About to install the requiring dependencies... ##

if [ "$os" == "UBUNTU" ]; then
    ## Upgrading Ubuntu to the latest Sendmail Version. ##
    apt-get install -y sendmail mailutils sasl2-bin > /dev/null 2>&1
elif [ "$os" == "CENTOS" ]; then
    ## Upgrading CentOS to the latest Sendmail Version. ##
    yum -y install sendmail mailutils mailx sendmail-bin sendmail-cf cyrus-sasl-plain
else
    Invalid Flavor of Linux
    exit
fi
echo -e ' \t '
## Create Gmail authentication file in a folder in which you will add Gmail user name and password.
echo -e ' \t '
mkdir $authInfoPath
cd $authInfoPath
echo "AuthInfo: \"U:root\" \"I:$email\" \"P:$password\"" >> $idpass
makemap hash $idpass < $idpass
chmod 700 $authInfoPath
echo -e ' \t '
echo -e ' \t '
echo "## Gmail Authentication Info injection complete. ##"

echo "Backing up Sendmail config File."
cp $sendmail $sendmail.$date
echo "Injecting Gmail Relay Code into sendmail.mc file."

cat <<'eof'  >/tmp/gmail.conf
# Adding config for gmail #
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/gmail-idpass.db')dnl
# End config for gmail #
eof

if [ "$os" == "UBUNTU" ]; then
    sed -i $'/MAILER_DEFINITIONS/{e cat /tmp/gmail.conf\n}' $sendmail
elif [ "$os" == "CENTOS" ]; then
    sed -i '/dnl MASQUERADE_DOMAIN(mydomain.lan)dnl/r /tmp/gmail.conf' $sendmail
fi

echo -e ' \t '
echo "## Injection of Gmail Relay Code into Sendmail.mc Complete. ##"

echo "Rebuilding Sendmail & Restarting Service."
make -C /etc/mail
/etc/init.d/sendmail restart

if [ "$os" == "UBUNTU" ]; then
    mail="mail.log"
elif [ "$os" == "CENTOS" ]; then
    mail="maillog"
fi

case "$response" in
    [yY][eE][sS]|[yY])
        echo -e  "Mail Body - Test Message" | mail -s "TMBC is Mail Sending from CLI" -r  $email  $personal

        [[ "$choice" == [Yy]* ]] && tail -n 10 /var/log/$mail || echo "Skipping log tail!"
        ;;
    *)
        echo "Skipping send test!"
        ;;
esac