一、sendEmail简介
sendEmail是一个轻量级,命令行的SMTP邮件客户端。如果你需要使用命令行发送邮件,那么sendEmail是非常完美的选择:使用简单并且功能强大.这个被设计用在php、bash
perl和web站点使用。
以上是sendEmail的简单介绍,千万不要和sendmail搞混掉了。用了sendEmail你将不在喜欢sendmail了.
二、安装配置sendEmail
sendEmail下载地址:
http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
下载sendEmail
[[email protected] ~]# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz --2017-06-08 05:07:01-- http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz 正在解析主机 caspian.dotconf.net... 69.164.196.234 正在连接 caspian.dotconf.net|69.164.196.234|:80... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:29740 (29K) [application/x-gzip] 正在保存至: “sendEmail-v1.56.tar.gz” 100%[=========================================================================================================================================================================>] 29,740 126K/s in 0.2s 2017-06-08 05:07:07 (126 KB/s) - 已保存 “sendEmail-v1.56.tar.gz” [29740/29740]) [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog sendEmail-v1.56.tar.gz zabbix-release-2.2-1.el6.noarch.rpm [[email protected] ~]#
安装:
解压文件
[[email protected] ~]# tar -zxvf sendEmail-v1.56.tar.gz
copy--sendEmail到/usr/local/bin/并赋予权限
[[email protected] ~]# cd sendEmail-v1.56 [[email protected] sendEmail-v1.56]# ls CHANGELOG README README-BR.txt sendEmail sendEmail.pl TODO [[email protected] sendEmail-v1.56]# cp sendEmail /usr/local/bin [[email protected] sendEmail-v1.56]# cd /usr/local/bin/ [[email protected] bin]# ls sendEmail [[email protected] bin]# chmod 755 sendEmail
sendEmail命令选项:
创建zabbix服务器端发送邮件脚本
修改/etc/zabbix/zabbix_server.conf
AlertScriptsPath=
/etc/zabbix/alertscripts
修改后重启zabbix server
[[email protected] zabbix]# service zabbix-server restart Shutting down Zabbix server: [确定] Starting Zabbix server: [确定] [[email protected] zabbix]#
创建发送邮件脚本
[[email protected] alertscripts]# vi SendEmail.sh # Email: #!/bin/bash # # Filename: SendEmail.sh # Revision: 1.0 # Date: 2017/06/07 # Author: wangxb # Email: # Description: zabbix邮件告警脚本 # Notes: 使用sendEmail # # 脚本的日志文件 LOGFILE="/tmp/Email.log" :>"$LOGFILE" exec 1>"$LOGFILE" exec 2>&1 SMTP_server=‘smtp.163.com‘ # SMTP服务器,变量值需要自行修改 username=‘[email protected]‘ # 用户名,变量值需要自行修改 password=‘zabbix‘ # 密码,变量值需要自行修改 from_email_address=‘[email protected]‘ # 发件人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
然后执行下面的命令,设置脚本和日志文件权限
[[email protected] alertscripts]# cd /tmp/ [[email protected] tmp]# mkdir Email.log [[email protected] tmp]# ll 总用量 64 drwxr-xr-x 2 root root 4096 6月 8 05:43 Email.log [[email protected] tmp]# chown zabbix:zabbix Email.log [[email protected] tmp]# chown zabbix:zabbix /etc/zabbix/alertscripts/SendEmail.sh [[email protected] tmp]# chmod -x /etc/zabbix/alertscripts/SendEmail.sh [[email protected] tmp]# chown -R zabbix:zabbix /etc/zabbix/alertscripts/
测试发送邮件
[[email protected] ~]# bash etc/zabbix/alertscripts/SendEmail.sh [email protected] "测试邮件标题" "测试邮件内容" bash: etc/zabbix/alertscripts/SendEmail.sh: 没有那个文件或目录 [[email protected] ~]# bash /etc/zabbix/alertscripts/SendEmail.sh [email protected] "测试邮件标题" "测试邮件内容" /etc/zabbix/alertscripts/SendEmail.sh: line 13: /tmp/Email.log: 是一个目录 /etc/zabbix/alertscripts/SendEmail.sh: line 14: /tmp/Email.log: 是一个目录 + /usr/local/bin/sendEmail -s smtp.163.com -xu [email protected]163.com -xp 123456 -f [email protected]163.com -t 115231245@qq.com -u $‘\262\342\312\324\323\312\274\376\261\352\314\342‘ -m $‘\262\342\312\324\323\312\274\376\304\332\310\335‘ -o message-content-type=text -o message-charset=gb2312 Jun 08 05:53:14 localhost sendEmail[2185]: Email was sent successfully!
时间: 2024-10-12 20:25:02