cat mail.sh #!/bin/bash #-------------------------------------------------- # Created:2015-05-04 # Author:jimmygong # Mail:jimmygong@taomee.com # Function:shell mutt msmtp # Version:1.0 #-------------------------------------------------- [[ -e /lib/lsb/init-functions ]] && source /lib/lsb/init-functions [[ -e /etc/init.d/functions ]] && source /etc/init.d/functions set -o nounset mailsmtp="smtp.163.com" mailuser="8888888@163.com" username="${mailuser%@*}" mailpwd="123456" mailport="25" sdate="log$(date +%s)" tarcmd="tar xf" url="http://sourceforge.net" msmtptar="msmtp-1.4.21.tar.bz2" msmtpver=`echo $msmtptar|awk -F"-" ‘{print $2}‘|sed ‘s/.tar.bz2//g‘` msmtphead=`echo ${msmtptar%%-*}` msmtpurl="$url/projects/msmtp/files/msmtp/$msmtpver/$msmtptar" mutttar="mutt-1.5.22.tar.gz" muttver=`echo $mutttar|awk -F"-" ‘{print $2}‘|sed ‘s/.tar.gz//g‘` mutthead=`echo ${mutttar%%-*}` mutturl="$url/projects/mutt/files/mutt-dev/$mutttar" debianpkg=(libncurses-dev make gcc bzip2 xsltproc docbook-xsl lynx curl axel wget) centospkg=(gcc ncurses-devel make docbook-style-xsl.noarch curl wget) echosucc () { succstatus="[ Ok ]" printf "\033[32m $succstatus $* \033[0m\n" } echofail () { failstatus="[ Failure ]" printf "\033[31m $failstatus $* \033[0m\n" exit 1 } function echoresult () { if [[ $? == ‘0‘ ]] then echosucc else echofail fi } function installwait () { echo -n "Start Install." for ((i=0;i<3;i++)) do echo -n ".";sleep 1 done echo } function installpkg () { installwait if [[ -e /etc/debian_version ]] && cat /etc/issue|head -1 then echo -n "install package:" apt-get -y install ${debianpkg[@]} --force-yes > ~/$sdate 2>&1 echoresult elif [[ -e /etc/redhat-release ]] && cat /etc/issue|head -1 then echo -n "install package:" yum -y install ${centospkg[@]} > ~/$sdate 2>&1 echoresult else echo "Unknown Release:" exit 1 fi } function downloadpkg () { if [[ ! -e ~/$msmtptar ]] && [[ ! -e ~/$mutttar ]] then status=`curl -o /dev/null -s -m 10 --connect-timeout 10 -w "%{http_code}\n" $url` if [[ $status != ‘200‘ ]] then echo -n "url unavailable:" echofail fi echo -n "download $msmtphead:" wget $msmtpurl > ~/$sdate 2>&1 echoresult echo -n "download $mutthead:" wget $mutturl > ~/$sdate 2>&1 echoresult elif [[ ! -e ~/$msmtptar ]] then echo -n "download $msmtphead:" wget $msmtpurl > ~/$sdate 2>&1 echoresult elif [[ ! -e ~/$mutttar ]] then echo -n "download $mutthead:" wget $mutturl > ~/$sdate 2>&1 echoresult fi } function installmsmtppkg () { echo -n "tar $msmtphead:" $tarcmd ~/$msmtptar > ~/$sdate 2>&1 echoresult cd ~/$msmtphead-$msmtpver echo -n "$msmtphead configure:" ./configure --prefix=/usr/local/msmtp > ~/$sdate 2>&1 echoresult echo -n "$msmtphead make:" make > ~/$sdate 2>&1 echoresult echo -n "$msmtphead make install:" make install > ~/$sdate 2>&1 echoresult [[ -e /usr/bin/msmtp ]]||ln -s /usr/local/msmtp/bin/msmtp /usr/bin/ cd .. } function installmuttpkg () { echo -n "tar $mutthead:" $tarcmd ~/$mutttar > ~/$sdate 2>&1 echoresult cd $mutthead-$muttver echo -n "$mutthead configure:" ./configure --prefix=/usr/local/mutt > ~/$sdate 2>&1 echoresult echo -n "$mutthead make:" make > ~/$sdate 2>&1 echoresult echo -n "$mutthead make install:" make install > ~/$sdate 2>&1 echoresult [[ -e /usr/bin/mutt ]]||ln -s /usr/local/mutt/bin/mutt /usr/bin/ cd .. } function confmsmtpmutt () { echo -n "configure $msmtphead&&$mutthead:" [[ -e /usr/local/msmtp/etc ]] ||mkdir -p /usr/local/msmtp/etc [[ -e /usr/local/msmtp/log ]] ||mkdir -p /usr/local/msmtp/log [[ -e /etc/Muttrc ]] || cat > /etc/Muttrc << EOF set sendmail="/usr/local/msmtp/bin/msmtp" set use_from=yes set realname="${mailuser}" set editor="vim" EOF [[ -e ~/.msmtprc ]] || cat > ~/.msmtprc << EOF host ${mailsmtp} tls off auth plain from ${mailuser} user ${username} password ${mailpwd} EOF [[ -e ~/.muttrc ]] || cat > ~/.muttrc << EOF set sendmail="/usr/local/msmtp/bin/msmtp" set use_from=yes set from=${mailuser} set envelope_from=yes EOF [[ -e /usr/local/msmtp/etc/msmtprc ]] || cat > /usr/local/msmtp/etc/msmtprc << EOF defaults account ${username} host ${mailsmtp} from ${mailuser} auth login port ${mailport} tls off user ${mailuser} password ${mailpwd} account default : ${username} logfile /usr/local/msmtp/log/msmtp.log EOF echoresult } installpkg downloadpkg installmsmtppkg installmuttpkg confmsmtpmutt echo "Test: echo "OKOK"|mutt -s "OKOK" $mailuser" exit 0 ===============================说明================================== 执行效果 bash mail.sh Start Install.... Debian GNU/Linux 5.0 \n \l install package: [ Ok ] download msmtp: [ Ok ] download mutt: [ Ok ] tar msmtp: [ Ok ] msmtp configure: [ Ok ] msmtp make: [ Ok ] msmtp make install: [ Ok ] tar mutt: [ Ok ] mutt configure: [ Ok ] mutt make: [ Ok ] mutt make install: [ Ok ] configure msmtp&&mutt: [ Ok ] Test: echo OKOK|mutt -s OKOK 88888888@163.com 测试(收到邮件了) echo "OKOK"|mutt -s "OKOK" 88888888@163.com 如果是需要带附件的话 /usr/local/mutt/bin/mutt -s "aaaaa" 88888888@163.com -c 88888888@qq.com </root/stock/20150418.1429337805 -a /root/stock/20150418.1429337805 -s 邮件标题 -s <subj> specify a subject (must be in quotes if it has spaces) -c 抄送地址 -c <address> specify a carbon-copy (CC) address -a 是附件 -a <file> attach a file to the message /root/stock/20150418.1429337805为邮件正文 。 如果发送多个附件,需要在每个附件前加-a参数。 有问题就看这个日志里的信息log$(date +%s) 此脚本在centos6.6和debian6.0和debian5.0上都跑过
本文出自 “7928217” 博客,请务必保留此出处http://7938217.blog.51cto.com/7928217/1641803
原文地址:http://7938217.blog.51cto.com/7928217/1641803