一、概述
sendEmail是一个轻量级,命令行的SMTP邮件客户端。如果你需要使用命令行发送邮件,那么sendEmail是非常完美的选择:使用简单并且功能强大.这个被设计用在php、bash、perl和web站点使
二、sendEmail安装及配置
1、需要安装依赖包
yum install perl-Net-SSLeay perl-IO-Socket-SSL -y
2、下载sendEmial包
cd /usr/local/src/
wget -c http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
3、解压安装
tar -xvf sendEmail-v1.56.tar.gz
cd sendEmail-v1.56/
cp -a sendEmail /usr/local/bin/
chmod +x /usr/local/bin/sendEmail
4、执行sendEmail查看帮助信息
/usr/local/bin/sendEmail
sendEmail-1.56 by Brandon Zehm <caspian@dotconf.net>
Synopsis: sendEmail -f ADDRESS [options]
Required:
-f ADDRESS from (sender) email address
* At least one recipient required via -t, -cc, or -bcc
* Message body required via -m, STDIN, or -o message-file=FILE
Common:
-t ADDRESS [ADDR ...] to email address(es)
-u SUBJECT message subject
-m MESSAGE message body
-s SERVER[:PORT] smtp mail relay, default is localhost:25
Optional:
-a FILE [FILE ...] file attachment(s)
-cc ADDRESS [ADDR ...] cc email address(es)
-bcc ADDRESS [ADDR ...] bcc email address(es)
-xu USERNAME username for SMTP authentication
-xp PASSWORD password for SMTP authentication
Paranormal:
-b BINDADDR[:PORT] local host bind address
-l LOGFILE log to the specified file
-v verbosity, use multiple times for greater effect
-q be quiet (i.e. no STDOUT output)
-o NAME=VALUE advanced options, for details try: --help misc
-o message-content-type=<auto|text|html>
-o message-file=FILE -o message-format=raw
-o message-header=HEADER -o message-charset=CHARSET
-o reply-to=ADDRESS -o timeout=SECONDS
-o username=USERNAME -o password=PASSWORD
-o tls=<auto|yes|no> -o fqdn=FQDN
Help:
--help the helpful overview you're reading now
--help addressing explain addressing and related options
--help message explain message body input and related options
--help networking explain -s, -b, etc
--help output explain logging and other output options
--help misc explain -o options, TLS, SMTP auth, and more
4、sendEmail 邮件测试
参数说明
/usr/local/bin/sendEmail #命令主程序
-f from@test.cn #发件人邮箱
-t to@test.cn #收件人邮箱
-s smtp.test.cn #发件人邮箱的smtp服务器
-u "MY测试邮件主题" #邮件的标题
-o message-content-type=html #邮件内容的格式,html表示它是html格式
-o message-charset=utf8 #邮件内容编码
-xu from@test.cn #发件人邮箱的用户名
-xp $passwd #发件人邮箱密码
-m "MY测试的邮件内容" #邮件的具体内容
邮件测试
/usr/local/bin/sendEmail -f my@test.com -t to@test.com -s smtp.test.cn -u "MY测试邮件主题" -o message-content-type=html -o message-charset=utf8 -xu my@test.com -xp $passwd -m "MY测试的邮件内容"
三、Zabbix告警配置及告警测试
1、zabbix自定义的指定目录
grep '^[a-Z]' /etc/zabbix/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
cd /usr/lib/zabbix/alertscripts
2、sendEmail脚本
vim sendEmail.sh
#!/bin/bash
LOGFILE="/tmp/Email.log"
:>"$LOGFILE"
exec 1>"$LOGFILE"
exec 2>&1
SMTP_server='smtp.test.cn' #SMTP服务器,变量值需要自行修改
username='my@test.com' #用户名,变量值需要自行修改
password='$passwd ' #密码,变量值需要自行修改
from_email_address='my@test.com' #发件人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'
set -x
$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
3、脚本权限
chmod 755 sendEmail.sh
touch /tmp/Email.log
chown zabbix.zabbix /tmp/Email.log
chown zabbix.zabbix sendEmail.sh
4、脚本测试
./sendEmail.sh my@test.com 我的主题测试 内容测试