Sendmail通过Gmail SMTP中继

时间:2018-06-03 18:34:29

标签: linux smtp google-cloud-platform sendmail emailrelay

我在Google Cloud中运行CentOS 7虚拟机,但在使用sendmail发送电子邮件时遇到困难。我已经按照以下帖子和其他许多帖子进行了访问,但结果仍然相同,即邮件从未到达目的地。

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/

我使用telnet会话连接到smtp-relay.gmail.com以从服务器测试它,并且能够连接并发送邮件,该邮件也到达目的地。

请原谅我的无知,我对Linux很新。当我使用mail -v命令发送邮件时,我得到下面的输出。

[root@backend-main-test mail]# echo "Just testing gmail relay" | mail -v -s "Sendmail gmail relay" myemailaddress@somewhere.com

myemailaddress@somewhere.com... 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:<root@backend-main-test.c.somecompany.internal> SIZE=252
250 2.1.0 <root@backend-main-test.c.somecompany.internal>... Sender ok
>>> RCPT To:<rualdventer@gmail.com>
>>> DATA
250 2.1.5 <myemailaddress@somewhere.com>... Recipient ok
354 Enter mail, end with "." on a line by itself
>>> .
250 2.0.0 w53Hxvqg002267 Message accepted for delivery
myemailaddress@somewhere.com... 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]#

它在结尾处给出了“消息接受传递”的消息,但在开始时它表示连接到127.0.0.1(localhost),如果它不是连接到我指定的smtp-relay.gmail.com主机在sendmail.mc和auth / authinfo文件的配置中?

根据/ var / log / maillog执行那些命令时,它给出了“如上所述接受传递的消息”,并且中继被指定为127.0.0.1

2 个答案:

答案 0 :(得分:0)

我通过Gmail脚本为ubuntu和centos创建了一个sendmail ..您可以随意使用它。

#! /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

答案 1 :(得分:-1)

Sendmail通过127.0.0.1:25将消息中继到本地sendmail守护程序,以避免设置root身份。
[set root id sendmail造成了长期的安全问题。]

测试sendmail守护程序配置以详细模式发送测试消息以root身份并使用地图查找跟踪(authinfo查找)。

#!/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 -- myemailaddress@somewhere.com <<END
Subject: gmail test

gmail test
END