Shell发送邮件以HTML展示
有时候,监控一个系统需要在系统出现警告时通过shell发送邮件来通知相关的负责人。本文讲讲如何shell发送邮件。
一种是普通邮件,通过附件描述详细报告;一种是高级邮件(哈哈),本来就是告警,还不赶快看,还要慢慢下载附件打开看?所以将附件内容以HTML直接展示到正文处,并通过样式标记警告。
1.安装sendEmail工具
下载、解压、配置环境变量
]# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz ]# tar -zxf sendEmail-v1.56.tar.gz -C SendEmail/ #配置环境变量这里不一样,不在profile文件里配置,而是拷贝到系统环境变量已有的路径中 ]# cd ~/myApp/SendEmail/ ]# cp sendEmail /usr/local/bin/
2.开启邮箱的SMTP
本文使用QQ邮件作为发送者
设置 -> 账户 -> 下图中开启
3.发送邮件
QQ邮箱对SMTP有如下说明:
#!/bin/bash RECIVER="[email protected]" SENDER="[email protected]" SENDER_USERNAME=964478654 #密码填写SMTP第三方客户端授权码 SENDER_PASSWD=*********** ATTACHMENT_PATH=/root/workspace/sh/attach_test.txt #如下图描述,作为邮件发送方应填这个hostname STMP_HOST=smtp.qq.com MESSAGEL_SUBJECT="Have Fun" MESSAGE_BODY="报警啊..." sendEmail -f $SENDER -t $RECIVER -a $ATTACHMENT_PATH \ -s $STMP_HOST -xu $SENDER_USERNAME -xp $SENDER_PASSWD \ -u $MESSAGEL_SUBJECT -m $MESSAGE_BODY \ -o message-charset=utf-8
发送后报错了:
******************************************************************* 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.
意思是SSL版本不对,原因是在cent7中Perl的版本5.16,而应该使用更低版本。或者不用SSL,也就需要在脚本最后一行的sendEmail
命令添加一个参数-o tls=no
sendEmail -f $SENDER -t $RECIVER -a $ATTACHMENT_PATH \ -s $STMP_HOST -xu $SENDER_USERNAME -xp $SENDER_PASSWD \ -u $MESSAGEL_SUBJECT -m $MESSAGE_BODY \ -o message-charset=utf-8 -o tls=no
发送成功了!
我擦,居然被当成诈骗钓鱼邮件。。
4.HTML版
邮箱直接展示html需要两个条件:
一是把要展示的数据使用html标签封装起来,就可以随意增加样式了;
二是在sendEmail
增加参数-o message-content-type=html
这里是根据系统内存情况发送系统邮件,当集群中机器的使用内存超过总内存80%的机器信息标红。
[root@NN1 sh]# cat memory_info ip consum free total 192.168.1.10 100 100 200 192.168.1.11 1 239 240 192.168.1.12 350 50 400 192.168.1.13 40 24 64 192.168.1.14 77 33 110 192.168.1.15 150 250 400
编写脚本
#!/bin/bash -x RECIVER="[email protected]" SENDER="[email protected]" SENDER_USERNAME=964478654 #密码填写SMTP第三方客户端授权码 SENDER_PASSWD=xuxjxankheuubcjd STMP_HOST=smtp.qq.com MESSAGEL_SUBJECT="Have Fun" HTML_PATH=html_path echo "<table>">$HTML_PATH echo "<thead><th>ip</th><th>consum</th><th>free</th><th>total</th></thead>">>$HTML_PATH echo "<tbody>">>$HTML_PATH create_tr() { i=2 lines=$(cat memory_info | wc -l) while [ $i -le $lines ] do j=1 ip=$(awk 'NR==i {print $j}' i=$i j=$j memory_info) let "j++" m=$(awk 'NR==i {print $j}' i=$i j=$j memory_info) let "j++" f=$(awk 'NR==i {print $j}' i=$i j=$j memory_info) let "j++" t=$(awk 'NR==i {print $j}' i=$i j=$j memory_info) tr="<tr>" #shell不能直接作小数运算 #更简单的运算写法是$[$m + 5] if [ $(expr $m \* 5) -gt $(expr $t \* 4) ]; then tr="<tr style=\"background-color:red\">" fi echo $tr>>$HTML_PATH echo "<td>"$ip"</td>">>$HTML_PATH echo "<td>"$m"</td>">>$HTML_PATH echo "<td>"$f"</td>">>$HTML_PATH echo "<td>"$t"</td>">>$HTML_PATH echo "</tr>">>$HTML_PATH let "i++" done } create_tr echo "</tbody>">>$HTML_PATH echo "</table>">>$HTML_PATH MESSAGE_BODY=$(cat $HTML_PATH) sendEmail -f $SENDER -t $RECIVER -a $ATTACHMENT_PATH \ -s $STMP_HOST -xu $SENDER_USERNAME -xp $SENDER_PASSWD \ -u $MESSAGEL_SUBJECT -m $MESSAGE_BODY \ -o tls=no message-charset=utf-8 message-content-type=html
发送成功!
好玩吧~
参考文章:
https://blog.csdn.net/leshami...
https://my.oschina.net/u/4005...
https://blog.csdn.net/wz94732...
相关推荐
tianhuak 2020-11-24
huha 2020-10-16
lianshaohua 2020-09-23
laisean 2020-11-11
zhangjie 2020-11-11
大牛牛 2020-10-30
firefaith 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
以梦为马不负韶华 2020-10-20
CARBON 2020-10-20
彼岸随笔 2020-10-20
lianshaohua 2020-10-20
yutou0 2020-10-17
JohnYork 2020-10-16
xiaonamylove 2020-10-16
Julyth 2020-10-16
applecarelte 2020-10-16
ourtimes 2020-10-16