zabbix 3.2.7 在centos 7.4上使用gmail 发送邮件

记录了 zabbix 3.2.7 在 centos 7.4使用 gmail作为stmp服务器发送邮件

# find / -name sendEmail
# tar xvzf sendEmail-v1.56.tar.gz
sendEmail-v1.56/
sendEmail-v1.56/CHANGELOG
sendEmail-v1.56/README
sendEmail-v1.56/README-BR.txt
sendEmail-v1.56/TODO
sendEmail-v1.56/sendEmail
sendEmail-v1.56/sendEmail.pl
# cp sendEmail-v1.56/sendEmail /usr/local/bin/
# chmod +x /usr/local/bin/sendEmail

使用 163发送

# /usr/local/bin/sendEmail -f ***@163.com -t ***@163.com -s smtp.163.com -u "send by 123" -o message-content-type=html -o message-charset=utf8 -xu **** -xp **** -m "邮件发送成功,good"
Jun 25 12:08:29 sendEmail[16984]: Email was sent successfully!

使用 gmail 发送

 # /usr/local/bin/sendEmail -f ***@gmail.com -t ***@163.com -s smtp.gmail.com:587 -u "send by 123" -o message-content-type=html -o tls=yes -o message-charset=utf8 -xu **** -xp ***** -m "邮件发送成功,good" 

Jun 25 22:07:50  sendEmail[46015]: NOTICE => Authentication not supported by the remote SMTP server!
Jun 25 22:07:50  sendEmail[46015]: ERROR => Received:     530 5.7.0 Must issue a STARTTLS command first. r134sm7400690pfc.68 - gsmtp

在发送邮件前,登录 gmail邮箱,需要开启 “转发和POP/IMAP”,并生成授权码

163邮箱也类似,需要登录163邮箱,开启PMP/SMTP服务才可以发送邮件

# ping gmail.com
# telnet smtp.gmail.com 587
Trying **** ...
Connected to smtp.gmail.com.
Escape character is ‘^]‘.
220 smtp.gmail.com ESMTP u11sm8926238pjf.46 - gsmtp
quit
221 2.0.0 closing connection u11sm8926238pjf.46 - gsmtp
Connection closed by foreign host.

sendemail.sh

#!/bin/bash
SMTP_server=‘smtp.163.com‘    # SMTP服务器
username=‘t***@163.com‘     # 用户名
password=‘***‘              # 密码
from_email_address=‘****‘ # 发件人Email地址
to_email_address="$1"               # 收件人Email地址,zabbix传入的第一个参数
message_subject_utf8="$2"           # 邮件标题,zabbix传入的第二个参数
message_body_utf8="$3"              # 邮件内容,zabbix传入的第三个参数

# 转换邮件标题为GB2312,解决邮件标题含有中文,收到邮件显示乱码的问题。
message_subject_gb2312=`iconv -t GB2312 -f UTF-8 << EOF
$message_subject_utf8
EOF`
[ $? -eq 0 ] && message_subject="$message_subject_gb2312" || message_subject="$message_subject_utf8"

# 转换邮件内容为GB2312
message_body_gb2312=`iconv -t GB2312 -f UTF-8 << EOF
$message_body_utf8
EOF`
[ $? -eq 0 ] && message_body="$message_body_gb2312" || message_body="$message_body_utf8"

# 发送邮件
sendEmail=‘/usr/local/bin/sendEmail‘
$sendEmail -s "$SMTP_server" -xu "$username" -xp "$password" -f "$from_email_address" -t "$to_email_address" -u "$message_subject" -m "$message_body" -o message-content-type=text -o message-charset=gb2312

使用gmai 的smtp服务器

#将smtp服务器替换
smtp.gmail.com:587
#还需要再 sendmail加上 -o tls=yes
[root@zabbix alertscripts]# ./sendEmail.sh  *****@163.com  1111 hellotest
Jun 25 23:44:41  sendEmail[69900]: ERROR => No TLS support!  SendEmail can‘t load required libraries. (try installing Net::SSLeay and IO::Socket::SSL)
[root@zabbix alertscripts]# ./sendEmail.sh  ******@163.com  1111 hellotest
Jun 25 23:49:29  sendEmail[70082]: NOTICE => Authentication not supported by the remote SMTP server!
Jun 25 23:49:30  sendEmail[70082]: ERROR => Received:     530 5.7.0 Must issue a STARTTLS command first. a9sm12016545pjm.51 - gsmtp

经过google发现,即使安装了 也还是报错

 

yum install ‘perl(Net::SSLeay)‘ 
yum install ‘perl(IO::Socket::SSL)‘

此系统为 Centos 7.4,使用 sendEmail-v1.56.tar.gz 发送邮件,的perl版本问题

# ./sendEmail.sh  ******@163.com  1111 hellotest
*******************************************************************
 Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client
 is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER
 possibly with SSL_ca_file|SSL_ca_path for verification.
 If you really don‘t want to verify the certificate and keep the
 connection open to Man-In-The-Middle attacks please set
 SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application.
*******************************************************************
  at /usr/local/bin/sendEmail line 1906.
invalid SSL_version specified at /usr/share/perl5/vendor_perl/IO/Socket/SSL.pm line 444.
wget http://www.cpan.org/src/5.0/perl-5.10.0.tar.gz ##降低perl版本(不建议)
     tar -zxf perl-5.10.0.tar.gz
     cd perl-5.10.0
     ./configure.gnu -des -Dprefix=/usr/local/perl
     echo $?
     make
     make test
     make install
# mv /usr/bin/perl /usr/bin/perl.bak
# ln -s /usr/local/perl/bin/perl /usr/bin/perl
# perl -v

This is perl, v5.10.0 built for x86_64-linux

Copyright 1987-2007, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

修改了,发送邮件还是报错,后面排查 问题应该是 (try installing Net::SSLeay and IO::Socket::SSL) 这个2个版本与perl的版本不兼容问题

 

尝试用 ssmtp 发送邮件

yum --enablerepo=extras install epel-release
yum install ssmtp
yum install mailx
# ll /etc/ssmtp/ssmtp.conf 
-rw-r----- 1 root mail 1483 Oct 29  2014 /etc/ssmtp/ssmtp.conf

配置文件

# cat /etc/ssmtp/ssmtp.conf |grep -v ‘^$‘|grep -v ‘^#
root=***@gmail.com
mailhub=smtp.gmail.com:587
RewriteDomain=gmail.com
Hostname=***
FromLineOverride=YES
UseTLS=YES
TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt
UseSTARTTLSCert=YES
UseSTARTTLS=YES
AuthUser=****gmail.com
AuthPass=****
AuthMethod=LOGIN
# more /etc/ssmtp/revaliases
# sSMTP aliases
# 
# Format:    local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.
root:****@gmail.com:smtp.gmail.com:587
# chmod 640 /etc/ssmtp/ssmtp.conf
# chown root:mail /etc/ssmtp/ssmtp.conf
# echo "E-Mail zabbix test" | ssmtp -vvv ******@163.com
[<-] 220 smtp.gmail.com ESMTP h8sm9629217pfn.0 - gsmtp
[->] EHLO ***
[<-] 250 SMTPUTF8
[->] STARTTLS
[<-] 220 2.0.0 Ready to start TLS
[->] EHLO ***
[<-] 250 SMTPUTF8
[->] AUTH LOGIN
[<-] 334 VXNlcm5hbWU6
[->] Ym9sMTEwLm9ydXJvQGdtYWlsLmNvbQ==
[<-] 334 UGFzc3dvcmQ6
[<-] 235 2.7.0 Accepted
[->] MAIL FROM:<****@gmail.com>
[<-] 250 2.1.0 OK h8sm9629217pfn.0 - gsmtp
[->] RCPT TO:<***@163.com>
[<-] 250 2.1.5 OK h8sm9629217pfn.0 - gsmtp
[->] DATA
[<-] 354  Go ahead h8sm9629217pfn.0 - gsmtp
[->] Received: by *** (sSMTP sendmail emulation); Sat, 26 Jun 2021 17:00:17 -0400
[->] From: "root" <****@gmail.com>
[->] Date: Sat, 26 Jun 2021 17:00:17 -0400
[->] test
[->] 
[->] .
[<-] 250 2.0.0 OK  1624741219 h8sm9629217pfn.0 - gsmtp
[->] QUIT

使用 ssmtp发送成功

脚本

# more ssmtpemail.sh 
#!/bin/sh
to=$1
subject=$2
body=$3
echo "From:<****@gmail.com>
To: y****@163.com,***@163.com

Subject: From ***** Center: $subject

Something has gone wrong at service $body,Please check soon!!!

" | ssmtp  $to
# ./ssmtpemail.sh ****@163.com,****@163.com "mysql is down" "mysql 3306 port is down"

 

再此尝试用 gmail 发送邮件

sendEmail[5716]: ERROR => No TLS support!  SendEmail can‘t load required libraries. (try installing Net::SSLeay and IO::Socket::SSL)
Jun 25 23:31:28  yum[48987]: Installed: perl-Mozilla-CA-20130114-5.el7.noarch
Jun 25 23:31:28  yum[48987]: Installed: perl-Net-LibIDN-0.12-15.el7.x86_64
Jun 25 23:31:28  yum[48987]: Installed: perl-WWW-Curl-4.15-13.el7.x86_64
Jun 25 23:31:28  yum[48987]: Installed: perl-Net-SSLeay-1.55-6.el7.x86_64
Jun 25 23:31:28  yum[48987]: Installed: perl-IO-Socket-IP-0.21-5.el7.noarch
Jun 25 23:31:28  yum[48987]: Installed: perl-IO-Socket-SSL-1.94-7.el7.noarch
Jun 25 23:31:28  yum[48987]: Installed: 1:openssl-perl-1.0.2k-21.el7_9.x86_64

 

仍是是此错误,但是包都已经安装。于是再 centos 6.10上使用脚本发送邮件。发现没有任何问题。

此问题应该就是之前 反映的 perl的版本与 系统版本的问题

即使降低 perl的版本 也不行,gmail 必须要求,tls=yes. gmal发送就是有点问题.如果要使用gmail,建议用 centos6发送邮件。

 

zabbix 3.2.7 在centos 7.4上使用gmail 发送邮件

上一篇:IO多路复用的三种机制Select、Poll、Epoll


下一篇:react Fiber 架构原理