上次的ruby考勤那个, 因为天天发邮件, 人事觉得烦, 刚好现在在折腾Python, 于是用这个重写了...
这个发邮件的, 可以发附件,图片... 记录一下
#!/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 = ‘Some HTML text and an image.
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‘, ‘‘ % (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
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"))
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
hour += 0.0
elif 15 <= sec
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)