shell mutt msmtp 发邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
我的博客已迁移到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).        #不显示指令执行过程









本文转自 xdoujiang 51CTO博客,原文链接:http://blog.51cto.com/7938217/1641803,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值