设为首页 收藏本站
查看: 1137|回复: 0

[经验分享] 一个在Linux上用sendmail发送带附件的邮件的脚本

[复制链接]

尚未签到

发表于 2015-11-25 11:31:49 | 显示全部楼层 |阅读模式
  最近项目中要写个自动测试脚本,每天半夜跑,把结果要发到邮箱里。
  sendmail不发附件很简单,但是这次要把结果作为附件发送出去。
  在中文网页里搜了半天,一个也没找到。
  最终在E文网页里找到了这样一个脚本,自己用了感觉很好用,发在这里方便后来人。
  
  #!/bin/bash#set +x## jail -> shell script for sending mime email enclosures from command line## Usage : jail [options] email-address file-to-be-sent#                             $1              $2## John Roebuck - 11/08/97## John Roebuck - 29/01/98 - Add options to allow optional subject and urgent.# John Roebuck - 18/03/98 - Get Hostname of machine and user.# John Roebuck - 26/05/98 - Allow multiple files to be sent.# John Roebuck - 11/12/98 - Remove all MTMS options and requirements,#                           also added -v for verbose (debug) sendmail option.# John Roebuck - 17/12/98 - Added -b option of uuencoding binary files.# John Roebuck - 22/12/98 - Added -r option of delivery receipt required.version="jail 1.4 by John Roebuck 17/12/98"## Get command line options#bflag=0rflag=0sflag=0uflag=0vflag=0while getopts "brs:uv" argdocase $arg inb)     bflag=1;;r)     rflag=1;;s)     sflag=1sval="$OPTARG";;u)     uflag=1;;v)     vflag=1;;## Display syntax if ? passed as an option in the command line#?)     echo " "echo "Usage : jail [options] email-address files-to-be-set"echo " "echo "        options are  -b for binary files"echo "                     -r delivery receipt required"echo "                     -s (subject of mail message)"echo "                     -u Urgent"echo "                     -v Verbose sendmail (debug)"echo "        ("$version")"echo " "exit 2;;esacdone## Set urgent flag to 1 if urgent option is found#if [ $uflag = 1 ]; then urgent="urgent"else urgent="normal"fi## Include subject from command line if subject option is found#if [ $sflag = 1 ]; then subject=$svalelse subject=" "fishift $(($OPTIND -1))## Display syntax if less than 2 command line parameters are found# after the command line options. Require email address and 1 file# as a minimum.#if [ $# -lt 2 ]then echo " "echo "Usage : jail [options] email-address files-to-be-sent"echo " "echo "        options are  -b for binary files"echo "                     -r delivery receipt required"echo "                     -s (subject of mail message)"echo "                     -u Urgent"echo "                     -v Verbose sendmail (debug)"echo "        ("$version")"echo " "exitfi## set -f stops * being expanded within the shell script. (ksh option)#set -f    ## Now get the send to email address#sendto=$1shift## Find out how many files to send as enclosures in this email message#attno=$#if [ $attno = 1 ]; then attmess="enclosure"else attmess="enclosures"fi## Format the variable $now as "Friday 18 December 1998 16:22"#xday=`date +%a`xdayno=`date +%d`xmonth=`date +%b`xyear=`date +%Y`xtime=`date +%H:%M:%S`now=$xday", "$xdayno" "$xmonth" "$xyear" "$xtime## Create message boundary number based on process id#boundary=JPR$$## Create message id number based on date and process id#messid=`date +%d%m%y`$$## Create temporary file based on date time and process id#tempfile=/tmp/jail-`date +%d%m%y%H%M%S`$$touch $tempfile## Get sender details eg user name and description, machine hostname## sender login is the login name of the current user account#senderlogin=`whoami`## Domain is taken from the domain field in /etc/resolv.conf file#senderdomain=`grep domain /etc/resolv.conf | cut -f2`## Sender address is login name @ machine hostname . domain#senderaddress=$senderlogin"@"`hostname`"."$senderdomain## Sender name is taken from the login name description field in /etc/passwd#sendername=`grep $senderlogin /etc/passwd |cut -d: -f5`## Create header part of email file#echo "From: /""$sendername"/" <"$senderaddress">" >> $tempfileecho "To: "$sendto >> $tempfileecho "Date: "$now >> $tempfileecho "Mime-Version: 1.0 "$version >> $tempfileecho "Content-Type: Multipart/Mixed; boundary=Message-Boundary-"$boundary >> $tempfileecho "Subject: "$subject>> $tempfile## Is a delivery receipt required ?#if [ $rflag = 1 ]; thenecho "Return-Receipt-To: /""$sendername"/" <"$senderaddress">" >> $tempfilefiecho "Priority: "$urgent >> $tempfileecho "Message-Id: <"$messid"."$senderdomain >> $tempfileecho "Status: RO" >> $tempfileecho "" >> $tempfileecho "" >> $tempfileecho "--Message-Boundary-"$boundary >> $tempfileecho "Content-type: text/plain; charset=US-ASCII" >> $tempfileecho "Content-transfer-encoding: 7BIT" >> $tempfileecho "Content-description: Read Me First" >> $tempfileecho "" >> $tempfileecho "" >> $tempfile## Create mail message body part of email file#echo "Hello" >> $tempfileecho "" >> $tempfileecho "Please find "$attno" '"$attmess"' to this email message :- " >> $tempfileecho "" >> $tempfile## Generate file information for each enclosure. The information is in the# format : File name 1  aix.doc#          Produced on  16 Dec at 18:59#          File size    33469 bytes.#messno=0while [ $attno -gt $messno ]domessno=`expr $messno + 1`filename[$messno]=$1shiftreport1="File name "$messno"  "${filename[$messno]}report2=`ls -l "${filename[$messno]}" |awk '{print "" "Produced on  "$6 " "  $7 " at "  $8 "" }'`report3=`ls -l "${filename[$messno]}" |awk '{print "" "File size    "$5" bytes." "" }'`echo "$report1" >> $tempfileecho "$report2" >> $tempfileecho "$report3" >> $tempfileecho "" >> $tempfiledoneecho "" >> $tempfileecho "" >> $tempfileecho "Regards "$sendername" ("$senderaddress")" >> $tempfileecho "" >> $tempfile## For each file, create enclosure for the file.#messno=0while [ $attno -gt $messno ]domessno=`expr $messno + 1`## Check from command line if files are ascii or binary#if [ $bflag = 0 ]; then## Add extra chr$ to ascii files for unix to dos conversion. This# means reading in each line of the text file and writing it out to# the temp file with a charage return character added to the end of# each line.# Unforntunately ksh read will ignore leading spaces at the beginning# of each line.#echo "--Message-Boundary-"$boundary >> $tempfileecho "Content-type: Application/Octet-Stream; name=${filename[$messno]}; type=Text" >> $tempfileecho "Content-description: attachment; filename=${filename[$messno]}" >> $tempfileecho "" >> $tempfile{while read line_datadoecho "$line_data""" >> $tempfiledone } < ${filename[$messno]}else## uuencode binary mail#echo "--Message-Boundary-"$boundary >> $tempfileecho "Content-type: Application/Octet-stream; name=${filename[$messno]}; type=Binary" >> $tempfileecho "Content-disposition: attachment; filename=${filename[$messno]}" >> $tempfileecho "Content-transfer-encoding: X-UUencode" >> $tempfileecho "" >> $tempfileuuencode ${filename[$messno]} ${filename[$messno]} >> $tempfilefidone##  Send email message straight to sendmail. Use -v option if debug#  option has been set at the command line.#if [ $vflag = 1 ]; then /usr/lib/sendmail -v $sendto < $tempfileelse /usr/lib/sendmail $sendto < $tempfilefi## Remove temp file#rm $tempfile

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-143361-1-1.html 上篇帖子: Linux -- 测试sendmail邮件发送 下篇帖子: AIX sendmail 通过其他邮箱发送邮件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表