一个shell的邮件发送程序

定义2个文件,第一个就是发送程序

 

#!/bin/sh
# linux shell:send mail with smtp auth:auth mathod-auth login
# this shell had been test in contos5.1
# test by xiutuo
# define mail sender,reciver,subjest,mail content , mail sever,smtp auth user and user's password

# define mail sender
sender=$1

#define mail reciver
reciver=$2

# define mail subject(title)
subject=$3

# define mail content
email_content_txt=$4
 
# define mail server
mailserver='smtp.163.com'

#
# define smtp auth user:mail user(sender)
# username with base64 encode,please translate with base64 tools,
# or with command line: echo "yourname_or_password" | base64
# but this command aways not work perfert.I still dont know why.
# so i suggest you to access this website to translate them online.
# http://cnac.vicp.cc/WebSites/PHP/base64.php
#
smtp_auth_user="d3V4aWFuQG0tdGHaltZS5jb20="
smtp_auth_pwd="aHR3dXhCue7pYWO4hQCM="

#---------------------
if [ "$#" != 4 ]; then
echo
echo "Usage: $0 sender@mailsever.com recieve@domain.com subject mail_content_file"
echo
exit 3
fi
# create funcation for mail_content_file
mail_content(){
cat << EOF
From: <$sender>
To: <$reciver>
Subject: $subject
Date: `date` +0800
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"; format=flowed
X-Mailer: centos5 xiutuo mail shell with smtp auth
X-MimeOLE: Powered by xiutuo

EOF
test -r $email_content_txt && cat $email_content_txt
}
send_mail(){
(
sleep 5
for comm in "ehlo $mailserver" "auth login" "$smtp_auth_user" "$smtp_auth_pwd" "mail from:$sender" "rcpt to:$reciver" "data"
do
echo "$comm";sleep 3
done
mail_content
sleep 3;echo "."
)| telnet $mailserver 25
}
send_mail

 

 

第二个就是发送脚本,加入参数

 

#!/bin/sh
#脚本名:mon-disk
#定义139邮箱,可以发送短信通知,免费的
rpct_to='aaaaaa@xxx.com'

#定义发件箱
mail_from='xxxx@163.com'
#邮件标题
mail_title='磁盘检测报告'
mail_demo=/opt/sbin/automail
#磁盘报告
disk_free_report='/opt/sbin/disk_report'
disk_status_now='/opt/sbin/disk_status_now'
lines='----------------------------------------------'
#磁盘报告标题
title='磁盘监测,时间:'
#服务器名
servername='raid5'

#服务器ip
ip='xxx.xxx.xxx.xxx'
context1='超过80%使用的磁盘情况,如下,请及时清理'
context2='已用%  挂载点'
cat /dev/null > $disk_free_report
ts=`(date +%Y-%m-%d-%H:%M:%S)`
#当有磁盘容量使用超过70的,就把对应的挂载点取出,并发邮件通知
df -P | awk -F'%' '{print $1,$2}' | awk 'NR>1 && $5>80 {print $5,$6}' > $disk_status_now
num=`cat $disk_status_now | wc -l`
if [ $num -gt 0 ];then
         echo -e "$title$ts\n$lines\n$servername\n$ip\n$context1\n$lines\n$context2\n" >>$disk_free_report
         cat $disk_status_now >>$disk_free_report
         echo -e "$lines\n" >>$disk_free_report
# 调用发送带认证的邮件脚本(免去了配置邮件服务器的麻烦)
         $mail_demo "$mail_from" "$rpct_to" "$mail_title" "$disk_free_report"
else
     echo "fine"
fi

 

转载于:https://www.cnblogs.com/yihang/archive/2010/12/23/1915309.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值