Enhanced version of back-up-text-files-using-mail-function
Also check mutt .
Also check mutt .
#!/bin/bash # send email with a file as the attachment # usage: # mailit email files # http://scriptdemo.blogspot.com # note: # some old versions do not support the attachment option # [tested with Heirloom mailx 12.5] function isEmail() { cEmail=`echo $1 | grep "^[a-zA-Z]\+\([._][a-zA-Z0-9]\+\)*@\([a-zA-Z0-9]\+\.\)\+[a-zA-Z]\{2,3\}$"` if [ ${#cEmail} -gt 0 ]; then echo "true" else echo "false" fi } function isServerOn() { myS=$1 myp=`ping -c 2 ${myS} 2>&1 | grep "% packet" | awk '{print $6*100}'` if [ ${#myp} -eq 0 ]; then echo "-1" #server: ${myS} is not valid elif [ ${myp} -eq 100 ]; then echo "0" #server: ${myS} is off else echo "1" #server: ${myS} is on or partly responses fi } if [ $# -le 1 ]; then echo "usage: mailit email file1 [file2] ..." exit fi #check email address targetEmail=$1 isValidEmail=`isEmail ${targetEmail}` [ ${isValidEmail} == "false" ] && echo "${targetEmail} is not a valid email address!!!" && exit #check smtp server smtpS=`echo ${targetEmail} | awk -F\@ '{print $2}'` smtpS="smtp.${smtpS}" isOnline=`isServerOn ${smtpS}` if [ ${isOnline} == "-1" ]; then echo "SMTP server: ${smtpS} is invalid. STOP" && exit elif [ ${isOnline} == "0" ]; then echo "SMTP server: ${smtpS} is offline. STOP" && exit fi #send email for (( nf=2; nf<=$#; nf++ )) do eval "myfile=\$${nf}" if [ -f ${myfile} ]; then echo "The attachment is ${myfile} sent by `whoami` from `hostname`" > tmpcont.mailtomea mail -s "[script] ${myfile}" -a ${myfile} ${targetEmail} < tmpcont.mailtomea && \ echo "send mail successfully" && \ rm -f tmpcont.mailtomea else echo "${myfile} dos not exist" fi done |
No comments:
Post a Comment