/
mail.py
65 lines (57 loc) · 2.29 KB
/
mail.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
"""
Mail类:
类名:Mail
功能:解析用户等级,向指定用户列表发送邮件
属性:user dict 告警用户等级字典,id邮箱
方法:
方法名:send_list
形参:level int 报警等级
返回值:tuple 告警人员邮箱列表
功能描述:通过报警等级,解析出需要发送的用户邮箱原组,然后返回
方法名:send_mail
形参:mail tuple 告警人员邮箱列表
message string 告警信息
level int 告警等级
返回值:Null
功能描述:给邮件列表发送告警信息
"""
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import email.utils
class Mail:
def __init__(self, user_dict, level_int,message_string):
self.server = "#设置服务器"
self.user = " #用户名 "
self.passwd = " #口令"
self.dict = user_dict #告警用户等级字典
self.level = level_int #报警等级
self.info = message_string #告警信息
def send_list(self):
match_data = []
for (key, value) in self.dict.items():
if key <= self.level:
match_data.append (value)
return match_data
def send_mail(self):
mail_tuple = self.send_list()
for i in range(len(mail_tuple)):
print mail_tuple[i]
message = MIMEText('告警信息...', 'plain', 'utf-8')
message['From'] = Header(u"服务器<%s>" %self.user)
message['To'] = Header(u"用户 <%s>" % mail_tuple[i])
message['Subject'] = Header(u"告警信息:%s"%self.info )
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(self.server, 25) # 25 为 SMTP 端口号
smtpObj.login(self.user,self.passwd)
smtpObj.sendmail(self.user, mail_tuple[i], message.as_string())
print "邮件发送成功"
except smtplib.SMTPException:
print "Error: 无法发送邮件"
if __name__ == '__main__':
data = {'1':'@qq.com','2':'zpan@linux.org'}
a= Mail(data,'1','warning')
a.send_mail()