一、sendEmail介绍
SendEmail is a lightweight,command line SMTP email client. If you have the need to send email from a command line,this
free program is perfect: simple to use and feature rich. It was designed to be used in bash scripts,batch files,Perl
programs and web sites,but is quite adaptable and will likely meet your requirements. SendEmail is written in Perl and is
unique in that it requires NO MODULES. It has an intuitive and flexible set of command-line options,making it very easy to
learn and use.
SendEmail is licensed under the GNU GPL,either version 2 of the License or (at your option) any later version.
[Supported Platforms: Linux,BSD,OS X,Windows 98,Windows NT,Windows 2000,& Windows XP]
sendmail是linux系统中一个邮箱系统,如果我们在系统中配置好sendmail就可以直接使用它来发送邮箱,sendEmail简单,轻量级,命令行,可伸缩,跨平台。
二、sendEmail安装
1 wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz 2 tar xvzf sendEmail-v1.56.tar.gz 3 cp sendEmail-v1.56/sendEmail /usr/local/bin/ 4 chmod +x /usr/local/bin/sendEmail
三、sendEmail的使用
这里通过编辑一个shell脚本来工zabbix监控程序来调用。
1 cd /apps/svr/zabbix/share/zabbix /alertscripts 2 vim sendEmail.sh 3 4 #!/bin/bash 5 SMTP_server='smtp.163.com'# SMTP服务器 6 username='zabbixxxxx@163.com' # 用户名 7 password='xxxxxxxxxx' # 密码 8 from_email_address='zabbixfish@163.com' # 发件人Email地址 9 to_email_address="$1" # 收件人Email地址,zabbix传入的第一个参数 10 message_subject_utf8="$2" # 邮件标题,zabbix传入的第二个参数 11 message_body_utf8="$3" # 邮件内容,zabbix传入的第三个参数 12 13 # 转换邮件标题为GB2312,解决邮件标题含有中文,收到邮件显示乱码的问题。 14 message_subject_gb2312=`iconv -t GB2312 -f UTF-8 << EOF 15 $message_subject_utf8 16 EOF` 17 [ $? -eq 0 ] && message_subject="$message_subject_gb2312" || message_subject="$message_subject_utf8" 18 19 # 转换邮件内容为GB2312 20 message_body_gb2312=`iconv -t GB2312 -f UTF-8 << EOF 21 $message_body_utf8 22 EOF` 23 [ $? -eq 0 ] && message_body="$message_body_gb2312" || message_body="$message_body_utf8" 24 25 # 发送邮件 26 sendEmail='/usr/local/bin/sendEmail' 27 $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 tls=no 28 29 [root@localhost alertscripts]# chmod +x sendEmail.sh 30 [root@localhost alertscripts]# ./sendEmail.sh xxxxxx@qq.com zabbix hello 31 Jul 21 13:31:30 localhost sendEmail[84369]: Email was sent successfully!
解释:
-f 表示发送者的邮箱
-t 表示接收者的邮箱
-s 表示SMTP服务器的域名或者ip
-u 表示邮件的主题
-xu 表示SMTP验证的用户名
-xp 表示SMTP验证的密码
-m 表示邮件的内容
-o message-charset 邮件字符集
-o message-content-type=text 表示邮件的内容格式为 text,另外还有html格式
四、故障以及解决方案
故障一现象:
# sendEmail -f from@163.com -t toi@qq.com -u "TestMail" -s smtp.163.com -xu cgh -xp passwd \ > -m "This is test mail " ******************************************************************* Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER together 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 415
故障一解决方案:
1 a、卸载现有的perl版本,安装老版本(不推荐) 2 b、增加参数-o tls=no 选项 3 # sendEmail -f from@163.com -t to@qq.com -u "TestMail" -s smtp.163.com -xu cgh -xp passwd \ 4 > -m "This is test mail from Robinson" -o tls=no 5 Sep 28 09:49:55 hn249 sendEmail[127791]: Email was sent successfully!
故障二现象:
1 # sendEmail -f chengguohua@htyjie.com -t leshami@qq.com -u "TestMail" -s smtp.exmail.qq.com -xu xxxxx -xp passwd \ 2 > -m "This is test mail from xxxx" -o tls=no 3 Sep 28 10:00:16 hn249 sendEmail[127844]: ERROR => ERROR => SMTP-AUTH: Authentication to smtp.exmail.qq.com:25 Failed.
故障二解决方案:
需要在163邮件设置一个客户端授权密码即可。
注:其实sendEmail是一个十分有用的程序,我们在这个地方用了它,其实别的地方也可以用,典型的好处就是你不需要每台机器都装sendmail,开启smtp服务.直接用现成的一台邮件服务器就行了,这无疑很大的加强了系统的安全性,也节约了资源.
原文链接:https://www.f2er.com/centos/376408.html