標籤:shell mutt msmtp
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="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 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 ]]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 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 -s /usr/local/msmtp/bin/msmtp /usr/bin/cd ..}function installmuttpkg () {echo -n "tar $mutthead:"$tarcmd ~/$mutttar > ~/$sdate 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 -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 << 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===============================說明==================================執行效果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]測試(收到郵件了)echo "OKOK"|mutt -s "OKOK" [email protected] 如果是需要帶附件的話/usr/local/mutt/bin/mutt -s "aaaaa" [email protected] -c [email protected] </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
shell mutt msmtp 發郵件