上次的ruby考勤那个, 因为天天发邮件, 人事觉得烦, 刚好现在在折腾Python, 于是用这个重写了...
这个发邮件的, 可以发附件,图片... 记录一下
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
|
#!/usr/bin/python
import
datetime
import
MySQLdb
import
smtplib
from
email.mime.multipart
import
MIMEMultipart
from
email.mime.text
import
MIMEText
from
email.mime.image
import
MIMEImage
csv_str
=
'datetime,name,start,over,hour\n'
name
=
{
1
:
"mei.han"
,
2
:
"xing.mei"
,
3
:
"you.yao"
,
6
:
"zing.ma"
,
9
:
"jing.cai"
}
csv_file
=
"/tmp/check_in.csv"
def
send_mail(filename
=
[], picname
=
[], content_txt
=
'
', content_html='
'):
smtpserver
=
'smtp.163.com'
username
=
'test@163.com'
password
=
'123123'
msg
=
MIMEMultipart()
msg[
'Subject'
]
=
'Check_in'
msg[
'From'
]
=
"test@163.com"
msg[
'To'
]
=
"me@163.com"
# attchment
if
len
(filename) >
0
:
for
i
in
filename:
att
=
MIMEText(
open
(i,
'rb'
).read(),
'base64'
,
'gf2312'
)
att[
"Content-Type"
]
=
'application/octet-stream'
att[
"Content-Disposition"
]
=
'attachment; filename="%s"'
%
i.split(
'/'
)[
-
1
]
msg.attach(att)
# attchment picture
if
len
(picname) >
0
and
content_html !
=
'':
for
i
in
range
(
0
,
len
(picname)):
#content_html = '<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image%s"><br>good!' % i
msg_content_html
=
MIMEText(content_html,
'html'
,
'gb2312'
)
msg.attach(msg_content_html)
with
open
(picname[i],
'rb'
) as f:
msgImage
=
MIMEImage(f.read())
msgImage.add_header(
'Content-ID'
,
'<image%s>'
%
(i
+
1
))
msg.attach(msgImage)
# content text
if
content_txt !
=
'':
msg_content_txt
=
MIMEText(content_txt,_subtype
=
'plain'
,_charset
=
'gb2312'
)
msg.attach(msg_content_txt)
# content html
if
content_html !
=
''
and
len
(picname)
=
=
0
:
msg_content_html
=
MIMEText(content_html,_subtype
=
'html'
,_charset
=
'gb2312'
)
msg.attach(msg_content_html)
smtp
=
smtplib.SMTP()
smtp.connect(
'smtp.163.com'
)
smtp.starttls()
#ssl send
smtp.login(username, password)
smtp.sendmail(msg[
'From'
], msg[
'To'
], msg.as_string())
smtp.quit()
def
check_kq(kq_time):
tmp_list
=
[]
csv_tmp
=
''
work
=
False
try
:
conn
=
MySQLdb.connect(host
=
'192.168.2.2'
,user
=
'kq'
,passwd
=
'123123'
,db
=
'test'
,port
=
3306
)
cur
=
conn.cursor()
for
i
in
name.keys():
max_time
=
kq_time
+
" 23:00:00"
min_time
=
kq_time
+
" 06:00:00"
cur.execute(
"select * from kqtime where userid=%s and time>\'%s\' and time<\'%s\'"
%
(i,min_time,max_time))
kqtime
=
cur.fetchall()
for
x,y,z
in
kqtime:
#num, time, userid
tmp_list.append(y)
if
len
(tmp_list)
=
=
0
:
csv_tmp
=
csv_tmp
+
(
"%s,%s,,,0"
%
(kq_time,name[i]))
+
"\n"
elif
len
(tmp_list)
=
=
1
:
if
int
(tmp_list[
0
].strftime(
"%H"
)) <
12
:
csv_tmp
=
csv_tmp
+
(
"%s,%s,%s,,0"
%
(kq_time,name[i],tmp_list[
0
].strftime(
"%H:%M:%S"
)))
+
"\n"
else
:
csv_tmp
=
csv_tmp
+
(
"%s,%s,,%s,0"
%
(kq_time,name[i],tmp_list[
0
].strftime(
"%H:%M:%S"
)))
+
"\n"
work
=
True
else
:
hour_str
=
str
(
max
(tmp_list)
-
min
(tmp_list))
hour
=
int
(hour_str.split(
':'
)[
0
])
sec
=
int
(hour_str.split(
':'
)[
1
])
if
hour >
3
:
hour
-
=
1
if
sec <
15
:
hour
+
=
0.0
elif
15
<
=
sec <
45
:
hour
+
=
0.5
elif
sec >
=
45
:
hour
+
=
1.0
work
=
True
csv_tmp
=
csv_tmp
+
(
"%s,%s,%s,%s,%s"
%
(kq_time,name[i],
min
(tmp_list).strftime(
"%H:%M:%S"
),
max
(tmp_list).strftime(
"%H:%M:%S"
),hour))
+
"\n"
tmp_list
=
[]
cur.close()
conn.close()
except
:
pass
if
work:
return
csv_tmp
else
:
return
''
for
i
in
range
(
1
,
8
)[::
-
1
]:
#一周分析一次
day_kq
=
check_kq((datetime.datetime.now()
-
datetime.timedelta(days
=
i)).strftime(
"%Y-%m-%d"
))
if
day_kq !
=
'':
csv_str
=
csv_str
+
check_kq((datetime.datetime.now()
-
datetime.timedelta(days
=
i)).strftime(
"%Y-%m-%d"
))
+
"\n"
with
open
(csv_file,
'w'
) as f:
f.write(csv_str)
send_mail(filename
=
[csv_file], content_txt
=
"Check_In"
)
os.remove(csv_file)
|
本文转自 nonono11 51CTO博客,原文链接:http://blog.51cto.com/abian/1606132,如需转载请自行联系原作者