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,如需转载请自行联系原作者
目录
相关文章
|
23天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
106 1
|
25天前
|
Java Shell
SpringBoot启动脚本Shell
SpringBoot启动脚本Shell
17 0
|
3天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
27 3
|
6天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
26 0
|
7天前
|
Shell
Shell脚本之流程控制语句
Shell脚本之流程控制语句
|
8天前
|
JSON 运维 监控
训练shell常用脚本练习(三)
【4月更文挑战第14天】shell代码训练(三)
26 1
|
12天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
143 0
|
12天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
136 0
|
16天前
|
弹性计算 Shell Linux
ecs服务器shell常用脚本练习(六)
【4月更文挑战第4天】shell代码训练(六)
109 0
|
21天前
|
弹性计算 Shell 应用服务中间件
ecs服务器shell常用脚本练习(四)
【4月更文挑战第4天】shell代码训练(四)
96 0

相关课程

更多