-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsendMail.py
More file actions
38 lines (29 loc) · 1.14 KB
/
sendMail.py
File metadata and controls
38 lines (29 loc) · 1.14 KB
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
# -*- coding:utf-8 -*-
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
from ReadJSON import *
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(),addr.encode('utf-8') if isinstance(addr, unicode) else addr))
mail_dict = read_config()
if mail_dict:
from_addr = mail_dict['Mail']['from_addr']
password = mail_dict['Mail']['password']
to_addr= mail_dict['Mail']['to_addr']
smtp_server = mail_dict['Mail']['smtp_server']
else:
print u"配置文件不存在或内容有误! 请检查后重试! "
def SendMailTo(subject, result):
msg = MIMEText('%s' % result, 'plain', 'utf-8')
msg['From'] = _format_addr(u'监控爬虫 <%s>' % from_addr)
msg['To'] = _format_addr(u'管理员 <%s>' % to_addr)
msg['Subject'] = Header(subject, 'utf-8').encode()
server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
if __name__ == '__main__':
SendMailTo('test', 'test')