cat mail.sh#!/bin/bash#--------------------------------------------------# created:2015-05-04# author:jimmygong# mail:[email protected]# 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/functionsset -o nounsetmailsmtp= "smtp.163.com" mailuser= "[email Protected] "Username=" ${mailuser%@*} "mailpwd=" 123456 "mailport=" "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 echosuccelse echofailfi}function installwait () {echo -n "Start install." for ((i=0;i<3;i++)) do echo -n "."; sleep 1doneecho}function installpkg () {installwaitif [[ -e /etc/debian_ Version ]] && cat /etc/issue|head -1then echo -n "Install package:" apt-get -y install ${debianpkg[@]} -- force-yes > ~/$sdate 2>&1 echoresultelif [[ -e /etc/redhat-release ]] && cat /etc/issue|head -1then echo -n "Install package:" yum -y install ${centospkg[@]} > ~/$sdate 2>&1 echoresultelse echo "Unknown release:" exit 1fi}function downloadpkg () {if [[ ! -e ~/$msmtptar ]] && [[ ! -e ~/$mutttar &nbsP;]] Then status= ' Curl -o /dev/null -s -m 10 --connect-timeout 10 -w "%{http_code}\n" $url if [[ $status != ' ]] 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 echoresultelif [[ ! -e ~/$msmtptar ]]then echo -n "download $msmtphead:" wget $msmtpurl > ~/$sdate 2>&1 echoresultelif [[ ! -e ~/$mutttar ]]then echo -n "download $mutthead:" wget $mutturl > ~/$sdate 2>&1 echoresultfi}function installmsmtppkg () {echo -n "tar $msmtphead:" $tarcmd ~/$msmtptar > ~/$sdate 2 >&1echoresultcd ~/$msmtphead-$msmtpverecho -n "$msmtphead configure:"./configure --prefix=/usr/local/msmtp > ~/$sdate 2>&1echoresultecho -n "$ Msmtphead make: "make > ~/$sdate 2>&1echoresultecho -n " $msmtphead make install: "make install > ~/$sdate 2>&1echoresult[[ -e / usr/bin/msmtp ]]| | LN&NBSP;-S&NBSP;/USR/LOCAL/MSMTP/BIN/MSMTP&NBSP;/USR/BIN/CD&NBSP, ...} function installmuttpkg () {echo -n "tar $mutthead:" $tarcmd ~/$mutttar > ~/$sdate &NBSP;2>&1ECHORESULTCD $mutthead-$muttverecho -n "$mutthead configure:"./configure --prefix=/usr/local/mutt > ~/$sdate 2>&1echoresultecho -n "$mutthead make:" make > ~/$sdate 2>&1echoresultecho -n "$mutthead make install:" make install > ~/$sdate 2>&1echoresult[[ -e /usr/bin/mutt ]]| | LN&NBSP;-S&NBSP;/USR/LOCAL/MUTT/BIN/MUTT&NBSP;/USR/BIN/&NBSP;CD&NBSP, ...} 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 << eofset sendmaIl= "/usr/local/msmtp/bin/msmtp" Set use_from=yesset realname= "${mailuser}" set editor= "Vim" EOF[[ -e ~/.msmtprc ]] | | cat > ~/.msmtprc << eofhost ${mailsmtp}tls offauth plainfrom ${mailuser}user ${username}password ${mailpwd}eof[[ -e ~/.muttrc ]] | | cat > ~/.muttrc << eofset sendmail= "/USR/LOCAL/MSMTP/BIN/MSMTP" set use_from=yesset from=${mailuser}set envelope_from=yeseof[[ -e /usr/local/msmtp/etc/ msmtprc ]] | | cat > /usr/local/msmtp/etc/msmtprc << eofdefaults account ${ username}host ${mailsmtp}from ${mailuser}auth loginport ${mailport}tls offuser $ {mailuser}password ${mailpwd}account default : ${username}logfile /usr/local/msmtp/ Log/msmtp.logeofechoresult}installpkgdownloadpkginstallmsmtppkginstallmuttpkgconfmsmtpmuttecho "test: echo " Okok "|mutt -s " Okok " $mailuser" exit 0==== =========================== description ================================== Execution effect Bash mail.sh start install .... Debian gnu/linux 5.0 \n \linstall 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 [email proteCTED] Test (Received mail) echo "Okok" |mutt -s "Okok" [email protected] /usr/local/mutt/bin/mutt -s "AAAAA" If you need to bring an accessory [email protected] -c [email protected] </root/stock/20150418.1429337805 -a /root/stock/ 20150418.1429337805-s Mail title -s <subj> specify a subject (must be in quotes if it has spaces)-c cc address -c <address > specify a carbon-copy (CC) address-a is an accessory -a <file> attach a file to the message/root/stock/20150418.1429337805 for message body . If you send multiple attachments, you need to add the-a parameter before each attachment. If there's a problem, look at the information in this log. log$ (date +%s) This script is CENTOS6.6 and debian6.0 and debian5.0 all ran.
This article is from the "7928217" blog, please be sure to keep this source http://7938217.blog.51cto.com/7928217/1641803
Shell Mutt Msmtp e-mail