-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend_mail.py
106 lines (94 loc) · 3.29 KB
/
send_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
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
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
from pyecharts import Bar, Line, Grid
from pyecharts import configure
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
import os
import sys
reload(sys)
sys.setdefaultencoding('utf8')
sys.path.append('..')
import smtplib
import traceback
import time
import csv
import xlwt
from datetime import datetime, timedelta
from jinja2 import Template
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('dalao_mail'))
base_path = os.getcwd()+'/'
mail_host = 'email.qq.com'
mail_from = '[email protected]'
mail_pwd = 'xxxxxxxxx'
me = ['[email protected]']
#echarts
def echart(echart_name):
reader = unicode_csv_reader(echart_name+'.csv')
data = list(reader)
data_t = map(list, zip(*data[1:]))
attr = data_t[0]
name = data[0]
for i in range(1,len(data_t)):
#print(name[i],attr,data_t[i])
bar.add(name[i],attr,data_t[i], is_stack=True)
bar.render(path=echart_name+'.png')
#插入图片
def addPic(msg, pic_file):
echart(pic_file)
fp = open(base_path + pic_file + '.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<'+pic_file+'>')
msg.attach(msgImage)
def addAttach(msg, file_path):
attach = MIMEText(open(base_path + file_path, 'rb').read(), 'base64', 'utf-8')
attach["Content-Type"] = 'application/octet-stream'
attach["Content-Disposition"] = 'attachment; filename='+file_path
msg.attach(attach)
def unicode_csv_reader(csv_path, dialect=csv.excel, **kwargs):
with open(base_path + csv_path) as f:
csv_reader = csv.reader(f, dialect=dialect, **kwargs)
for row in csv_reader:
yield [unicode(cell, 'utf-8') for cell in row]
def add_table(csv_list):
tables = {}
for csv_list in csv_lists:
reader = unicode_csv_reader(csv_list+'.csv')
tables[csv_list] = list(reader)
return tables
def make_email(to_list, subject, content, attach_paths):
msg = MIMEMultipart('related') ##采用related定义内嵌资源的邮件
msgtext = MIMEText(content,_subtype='html',_charset='utf-8') ##_subtype有plain,html等格式,避免使用错误
msg.attach(msgtext)
if len(attach_paths) != 0:
for attach_path in attach_paths:
addAttach(msg, attach_path)
msg['Subject'] = subject
msg['From'] = mail_from
msg['To'] = ";".join(to_list)
return msg
def sendMail(to_list, subject, content, attach_paths):
msg = make_email(to_list, subject, content, attach_paths)
try:
server = smtplib.SMTP(mail_host, 25)
server.ehlo()
server.login(mail_from, mail_pwd)
server.sendmail(mail_from, to_list, msg.as_string())
server.quit() ##断开smtp连接
print "邮件发送成功"
except Exception, e:
print "失败"+str(e)
if __name__=='__main__':
yesterday = sys.argv[1]
subject = 'echart test'
bar = Bar(subject)
attach_lists = []
csv_lists = ['b','c']
pic_lists = ['b']
tables = add_table(csv_lists)
template = env.get_template('template.html')
content = template.render(day=yesterday, data=tables, pic=pic_lists[0])
sendMail(me, subject, content, attach_lists, pic_lists)