linux shell脚本 发送multipart格式邮件
Posted On 2016年3月11日
sendmail 发送multipart格式邮件,例如发送html格式,有附件等。
分享国外某位的写的,很早一直在用(发送html格式邮件),不过抱歉忘了是哪位写的了。
#!/bin/bash #requires: basename,date,md5sum,sed,sendmail,uuencode function fappend { echo "$2">>$1; } YYYYMMDD=`date "+%Y-%m-%d %k:%M"` # CHANGE THESE TOEMAIL="[email protected];[email protected];[email protected];[email protected]"; FREMAIL="[email protected]"; SUBJECT="测试结果(172.16.2.38) - $YYYYMMDD"; MSGBODY=`cat /home/test/software/apache-jmeter-2.13/extras/auto.html`; ATTACHMENT="" MIMETYPE="image/png" #if not sure, use http://www.webmaster-toolkit.com/mime-types.shtml # DON'T CHANGE ANYTHING BELOW TMP="/tmp/tmpfil_123"$RANDOM; BOUNDARY=`date +%s|md5sum` BOUNDARY=${BOUNDARY:0:32} #FILENAME=`basename $ATTACHMENT` rm -rf $TMP; #cat $ATTACHMENT|uuencode --base64 $FILENAME>$TMP; #sed -i -e '1,1d' -e '$d' $TMP;#removes first & last lines from $TMP #DATA=`cat $TMP` # rm -rf $TMP; fappend $TMP "From: $FREMAIL"; fappend $TMP "To: $TOEMAIL"; fappend $TMP "Reply-To: $FREMAIL"; fappend $TMP "Subject: $SUBJECT"; fappend $TMP "Content-Type: multipart/mixed; boundary=\""$BOUNDARY"\""; fappend $TMP ""; fappend $TMP "This is a MIME formatted message. If you see this text it means that your"; fappend $TMP "email software does not support MIME formatted messages."; fappend $TMP ""; fappend $TMP "--$BOUNDARY"; fappend $TMP "Content-Type: text/html; charset=UTF-8; format=flowed"; fappend $TMP "Content-Transfer-Encoding: 7bit"; fappend $TMP "Content-Disposition: inline"; fappend $TMP ""; fappend $TMP "$MSGBODY"; fappend $TMP ""; fappend $TMP ""; #fappend $TMP "--$BOUNDARY"; #fappend $TMP "Content-Type: $MIMETYPE; name=\"$FILENAME\""; #fappend $TMP "Content-Transfer-Encoding: base64"; #fappend $TMP "Content-Disposition: attachment; filename=\"$FILENAME\";"; #fappend $TMP ""; #fappend $TMP "$DATA"; #fappend $TMP ""; #fappend $TMP ""; #fappend $TMP "--$BOUNDARY--"; #fappend $TMP ""; #fappend $TMP ""; #cat $TMP>out.txt cat $TMP|sendmail -t; rm $TMP;
此篇文章已被阅读2354 次