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
|
我的博客已迁移到xdoujiang.com请去那边和我交流
一、基础环境
1、版本
cat
/etc/debian_version
7.8
2、内核
uname
-r
3.2.0-4-amd64
3、ip(eth0)
10.1.10.185
4、具体日志
cat
3.log
aaa 10-29_10:30 29629395
test
bbb 10-29_10:36 180035
test
ccc 10-29_10:37 180035
test
ddd 10-29_10:37 580495
test
eee 10-29_10:37 580495
test
fff 10-29_10:46 180035
test
ggg 10-29_10:46 180035
test
5、mutt使用是发到163的 python的是在公司使用的
6、使用的python版本是2.7
二、具体代码
cat
mailsend.sh
#!/bin/bash
#--------------------------------------------------
#Author:jimmygong
#Email:jimmygong@taomee.com
#FileName:mailsend.sh
#Function:
#Version:1.0
#Created:2015-10-30
#--------------------------------------------------
sendip=
"10.1.10.185"
maillog=
"/root/3.log"
mailbox=
"11111111@163.com"
function
handlemail ()
{
sed
-i -e
'1i\title1\ttitle2\ttitle3\tdtitle4'
$maillog
sed
-i -e
's/^/<tr><td>/'
-e
's/\s\+/<\/td><td>/'
-e
's/\s\+/<\/td><td>/'
-e
's/\s\+/<\/td><td>/'
-e
's/$/<\/td><\/tr>/'
$maillog
sed
-i -e
'1i\<table>'
-e
'$a\</table>'
$maillog
sed
-i -e
'1i\<style type="text\/css">table,td,th{border:1px solid black;}<\/style>'
$maillog
/usr/local/mutt/bin/mutt
-s
"aaa $sendip"
-e
'set content_type="text/html"'
$mailbox < $maillog
#python mailsend.py $maillog "aaa $sendip"
}
handlemail
exit
0
PS:
1、如果使用mutt的话 那么请先参考以下链接安装
http:
//7938217
.blog.51cto.com
/7928217/1641803
2、如果使用python的话 那么使用以下python代码
|
1
|
cat
mailsend.py
|
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
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
#--------------------------------------------------
#Author:jimmygong
#Email:jimmygong@taomee.com
#FileName:mailsend.py
#Function:
#Version:1.0
#Created:2015-10-30
#--------------------------------------------------
import
sys
import
smtplib
from
email
import
encoders
from
email.mime.base
import
MIMEBase
from
email.mime.text
import
MIMEText
from
email.utils
import
COMMASPACE
from
email.mime.multipart
import
MIMEMultipart
def
send_mail(fro,to,subject,text,txttype
=
'html'
,files
=
[]):
msg
=
MIMEMultipart()
msg[
'From'
]
=
fro
msg[
'Subject'
]
=
subject
msg[
'To'
]
=
COMMASPACE.join(to)
if
txttype
=
=
"html"
:
msg.attach(MIMEText(text,
'html'
))
else
:
msg.attach(MIMEText(text))
for
file
in
files:
part
=
MIMEBase(
'application'
,
'octet-stream'
)
part.set_payload(
open
(
file
,
'r'
).read())
encoders.encode_base64(part)
part.add_header(
'Content-Disposition'
,
'attachment; filename="%s"'
%
os.path.basename(
file
))
msg.attach(part)
s
=
smtplib.SMTP()
s.connect(
'mail.shidc.taomee.com'
)
s.sendmail(fro,to,msg.as_string())
s.quit()
if
__name__
=
=
'__main__'
:
fro
=
'ops@taomee.com'
to
=
[
'jimmygong@taomee.com'
,]
subject
=
sys.argv[
2
]
with
open
(sys.argv[
1
],
"r"
) as f:
text
=
f.read()
txttype
=
'html'
files
=
[]
send_mail(fro,to,subject,text,txttype,files)
|
1
2
3
4
|
3、一些说明
sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始.
三、效果
|
本文转自 xdoujiang 51CTO博客,原文链接:http://blog.51cto.com/7938217/1708228,如需转载请自行联系原作者