1)什么是sendEmail
sendEmail是一个轻量级,命令行的SMTP邮件客户端(注意,不要跟sendmail混淆了)。如果你需要使用命令行发送邮件,那么sendEmail是非常完美的选择:使用简单并且功能强大,从此以后你就爱上了它。
2)安装sendEmail
[root@zabbix ~]# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz [root@zabbix ~]# tar -zxvf 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 [root@zabbix ~]# ls anaconda-ks.cfg bin conf sendEmail-v1.56 sendEmail-v1.56.tar.gz zabbix_agent.tar [root@zabbix ~]# cd sendEmail-v1.56/ [root@zabbix sendEmail-v1.56]# ls CHANGELOG README README-BR.txt sendEmail sendEmail.pl TODO [root@zabbix sendEmail-v1.56]# mv sendEmail /usr/local/bin/ [root@zabbix sendEmail-v1.56]# which sendEmail /usr/local/bin/sendEmail [root@zabbix sendEmail-v1.56]# /usr/local/bin/sendEmail -f molewan@163.com -t 314324506@qq.com -s smtp.163.com -u "我是邮件主题" -o message-content-type=html -o message-charset=utf8 -xu molewan@163.com -xp "这里需要邮箱的密码" -m "我是邮件内容"
Nov 11 09:23:22 zabbix sendEmail[7608]: Email was sent successfully!
注意:需要开启163的smtp功能
3)sendEmail常用参数说明
-f molewan@163.com 发件人邮箱
-s smtp.163.com 发件人邮箱的smtp服务器
-u "我是邮件主题" 邮件的标题
-o message-content-type=html 邮件内容的格式,html表示它是html格式
-o message-charset=utf8 邮件内容编码
-xu molewan@163.com 发件人邮箱的用户名
-xp “密码” 发件人邮箱密码
-m "我是邮件内容" 邮件的具体内容
[root@zabbix sendEmail-v1.56]# sendEmail –help
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的两个实际例子
a、结合zabbix进行邮件告警(脚本从网络上qicheng大神博客直接下来修改的)
#!/bin/bash # # Filename: SendEmail.sh # Revision: 1.0 # Date: 2014/07/04 # Author: Qicheng # Email: # Website: http://qicheng0211.blog.51cto.com/ # Description: zabbix邮件告警脚本 # Notes: 使用sendEmail # # 脚本的日志文件 LOGFILE="/tmp/Email.log" :>"$LOGFILE" exec 1>"$LOGFILE" exec 2>&1 SMTP_server='smtp.163.com' # SMTP服务器,变量值需要自行修改 username='wanlong860514@163.com' # 用户名,变量值需要自行修改 password='输入邮箱的密码' # 密码,变量值需要自行修改 from_email_address='wanlong860514@163.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
b、内网发送邮件
[root@C67-X64-A0 ~]# cat checknetwork.sh
#!/bin/bash #created by molewan for n in {10,11,12,20,21,22,191} do nmap -v -sP 10.203.$n.0/24 | grep -v 'host down' |egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'>/tmp/uplist$n.log done /usr/local/bin/sendEmail -f dfylmailtest@163.com -t dpwanl@dfl.com.cn -s smtp.163.com -u "network ALL" -o message-content-type=html -o message-charset=utf8 -xu dfylmailtest@163.com -xp LM@2008 -m "network ALL" -a /tmp/uplist*
执行脚本并查收看生成的文件:
[root@C67-X64-A0 ~]# bash checknetwork.sh Jul 29 14:35:14 c67-x64-a0 sendEmail[4166]: Email was sent successfully! [root@C67-X64-A0 ~]# ls /tmp/ exploit lost+found pulse-mFN57JFCgte9 uplist10.log uplist12.log uplist20.log uplist22.log keyring-R8TupG orbit-gdm pulse-n6rjDFCVr3Y6 uplist11.log uplist191.log uplist21.log virtual-root.ZiX1iv
原创文章,作者:Net21-冰冻vs西瓜,如若转载,请注明出处:http://www.178linux.com/26650