【python发送zabbix报警邮件,SSL版本】mailman.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
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
|
#!/usr/local/bin/python3
#
# via <Nosmo King> @ 20150210
# SSL only
'''
用法:
mailman.py "to" "subject" "body" "attachments"
说明:
[-] 1个收件人:
./mailman.py 'a@example.com' "test subject" "simple test content"
[-] 多个收件人:
./mailman.py "a@example.com;b@example.com" "test again" "another simple test"
[-] 带附件:
./mailman.py 'c@example.com' 'test1' 'test att' '/tmp/a.log' '/tmp/1.log'
--
'''
from
email.mime.text
import
MIMEText
from
email.mime.base
import
MIMEBase
from
email.mime.multipart
import
MIMEMultipart
from
email
import
encoders
import
smtplib, os, sys, logging, base64
# 发件人
email_from_1
=
{
'smtp'
:
'smtp.126.com'
,
'account'
:
'xxx@126.com'
,
'password'
:
'xxx'
,
'nickname'
:
'zbx_svr'
,
'greeting'
:
'Dear Sir'
}
# 发件人,备用
email_from_2
=
{
'smtp'
:
'smtp.126.com'
,
'account'
:
'xxx@126.com'
,
'password'
:
'xxx'
,
'nickname'
:
'zbx_svr_bak'
,
'greeting'
:
'Dear Sir'
}
# +---- logging ---+
logging_file
=
'/tmp/mailman.py.log'
logging.basicConfig(
level
=
logging.DEBUG,
format
=
'%(asctime)s [%(levelname)s]: %(message)s'
,
filename
=
logging_file,
filemode
=
'a'
,
)
def
delivering(s_from, s_to):
'''
s_from: (smtp, account, password, nickname, greeting)
s_to: (to, subject, body, attachments)
'''
#+---- logging ---+
print
(
"logging to"
, logging_file)
logging.info(
'''\
Now delivering..
+------------------------------+
from: {0} <{1}>
to: {3}
subject: {4}
content:
>>
{2},
{5}
>>
attachments:
{6}
+------------------------------+
'''
.
format
(s_from['nickname
'], s_from['
account
'], s_from['
greeting'],
s_to[
'to'
], s_to[
'subject'
], s_to[
'body'
], s_to[
'attachments'
]))
# email header
m
=
MIMEMultipart()
m[
'From'
]
=
'{0} <{1}>'
.
format
(s_from[
'nickname'
], s_from[
'account'
])
m[
'To'
]
=
';'
.join(s_to[
'to'
])
m[
'Subject'
]
=
s_to[
'subject'
]
# email body
content
=
MIMEText(
'''
{0},
{1}
'''
.
format
(s_from['greeting
'], s_to['
body
']), '
plain
', '
utf
-
8
')
m.attach(content)
# email attachments
for
filename
in
s_to[
'attachments'
]:
with
open
(filename,
'rb'
) as f:
addon
=
MIMEBase(
'application'
,
'octet-stream'
)
addon.set_payload(f.read())
encoders.encode_base64(addon)
addon.add_header(
'Content-Disposition'
, 'attachment; \
filename
=
"{0}"
'.
format
(os.path.basename(filename)))
m.attach(addon)
# send email
svr
=
smtplib.SMTP()
try
:
svr.connect(s_from[
'smtp'
])
svr.ehlo()
svr.starttls()
svr.ehlo()
#svr.set_debuglevel(1)
svr.login(s_from[
'account'
], s_from[
'password'
])
svr.sendmail(s_from[
'account'
], s_to[
'to'
], m.as_string())
retval
=
0
except
KeyboardInterrupt:
print
(
'[*] operation aborted!'
)
retval
=
-
1
except
Exception as err:
print
(
'[*] delivering err: {0}'
.
format
(err),
file
=
sys.stderr)
#+---- logging ---+
logging.warning(
'delivering: {0}'
.
format
(err))
retval
=
-
2
finally
:
svr.quit()
#+---- logging ---+
logging.info(
"mailman code: {0}"
.
format
(retval))
return
retval
def
usage():
print
(__doc__)
sys.exit(
2
)
if
__name__
=
=
'__main__'
:
if
len
(sys.argv) <
4
:
usage()
email_to
=
{}
email_to[
'to'
]
=
sys.argv[
1
].split(
';'
)
email_to[
'subject'
]
=
sys.argv[
2
]
email_to[
'body'
]
=
sys.argv[
3
]
email_to[
'attachments'
]
=
sys.argv[
4
:]
try
:
retval
=
delivering(email_from_1, email_to)
if
retval <
0
:
tips
=
"try again, using backup account to deliver.."
print
(tips)
#+---- logging ---+
logging.info(tips)
retval
=
delivering(email_from_2, email_to)
msg
=
'Mail delivering: '
msg
+
=
'Failed!'
if
retval
else
'Successful!'
print
(msg)
#+---- logging ---+
logging.info(msg)
except
Exception as err:
print
(
'[*] main err: {0}'
.
format
(err),
file
=
sys.stderr)
# logging
logging.warning(
'{0}'
.
format
(err))
|
本文转自 pcnk 51CTO博客,原文链接:http://blog.51cto.com/nosmoking/1594759,如需转载请自行联系原作者