您的位置:首页 > 运维架构 > Shell

shell mutt msmtp 发邮件

2015-05-04 17:22 369 查看
我的博客已迁移到xdoujiang.com请去那边和我交流
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="88888888@163.com"
username="${mailuser%@*}"
mailpwd="123456"
mailport="25"
sdate="log$(date +%s)"
tarcmd="tar xf"
msmtptar="msmtp-1.4.21.tar.bz2"
msmtpver=`echo $msmtptar|awk -F"-" '{print $2}'|sed 's/.tar.bz2//g'`
msmtphead=`echo ${msmtptar%%-*}`
msmtpurl="http://sourceforge.net/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="ftp://ftp.mutt.org/pub/mutt/$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
echo -n "check msmtpurl:"
wget -T 15 -q --spider $msmtpurl > ~/$sdate 2>&1
echoresult
echo -n "check mutturl:"
wget -T 15 -q --spider $mutturl > ~/$sdate 2>&1
echoresult
echo -n "download $msmtphead:"
wget --limit-rate=100k $msmtpurl > ~/$sdate 2>&1
echoresult
echo -n "download $mutthead:"
wget --limit-rate=100k $mutturl > ~/$sdate 2>&1
echoresult
elif [[ ! -e ~/$msmtptar ]]
then
echo -n "check msmtpurl:"
wget -T 15 -q --spider $msmtpurl > ~/$sdate 2>&1
echoresult
echo -n "download $msmtphead:"
wget $msmtpurl > ~/$sdate 2>&1
echoresult
elif [[ ! -e ~/$mutttar ]]
then
echo -n "check mutturl:"
wget -T 15 -q --spider $mutturl > ~/$sdate 2>&1
echoresult
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 ]
check msmtpurl: [ Ok ]
check mutturl: [ 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上都跑过

wget --limit-rate=100k                #限速下载
wget --spider                         #测试下载链接
-T,  --timeout=SECONDS                #设置超时时间
-q,  --quiet quiet (no output).       #不显示指令执行过程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mutt msmtp