python编写服务到期提醒脚本


#coding=utf-8
import MySQLdb
import MySQLdb.cursors
import time
import os
import shutil
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication

mail_host="mail.lib.1111.com"  #设置服务器
mail_user="111"    #用户名
mail_pass="7777"   #口令
mail_postfix="111.com"  #发件箱的后缀
apabifrom="111@111.com"
tpath = '/root/mailsend/'
ttime = time.strftime("%Y-%m-%d", time.localtime())
npath = tpath+ttime
print npath
if not os.path.exists(npath):
os.mkdir(npath)
else:
shutil.rmtree(npath)
os.mkdir(npath)

def send_mail(to_list,sub,content):  #to_list:收件人;sub:主题;content:邮件内容
me="apabi"+"<"+mail_user+"@"+mail_postfix+">"   #这里的hello可以任意设置,收到信后,将按照设置显示
msg = MIMEText(content,_subtype='html',_charset='utf-8')    #创建一个实例,这里设置为html格式邮件
msg['Subject'] = Header(sub,"utf-8")    #设置主题
msg['From'] = apabifrom
msg['To'] = ','.join(to_list)
# msg = MIMEMultipart()
# part = MIMEApplication(open(filepath,'rb').read())
# part.add_header('Content-Disposition', 'attachment', filename=file_name)
# msg.attach(part)
try:
s = smtplib.SMTP()
s.connect(mail_host)  #连接smtp服务器
s.login(mail_user,mail_pass)  #登陆服务器
s.sendmail(apabifrom, to_list, msg.as_string())  #发送邮件
s.close()
return True
except Exception, e:
print str(e)
return False

def mysql_select(days):
conn=MySQLdb.connect(host='localhost',user='222',passwd='333',db='33',port=3306,cursorclass = MySQLdb.cursors.DictCursor,charset='utf8')
cur = conn.cursor()
cur.execute("select a.org_id,DATE_FORMAT(a.read_end_date,'%%Y-%%m-%%d') as read_end_date,a.paper_name,b.org_name from ORG_AUTH_PAPER a,ORG b where a.org_id=b.org_id and to_days(a.read_end_date) - to_days(now())=%s and status=2" %days,)
#cur.execute("select a.org_id,DATE_FORMAT(a.read_end_date,'%Y-%m-%d') as read_end_date,a.paper_name,b.org_name from ORG_AUTH_PAPER a,ORG b where a.org_id=b.org_id LIMIT 100" )
alldata=cur.fetchall()
cur.close()
conn.close()
mail_lis=[]
order={}
org_id=[]
count=0
if alldata:
for order in alldata:
with open(npath+'/'+order['org_id']+'.txt','a') as filea:
filea.write('<tr><th>'+order['org_name']+'</th><th>'+order['paper_name']+'</th><th>'+order['read_end_date']+'</th></tr>'+"\n")
org_id.append(order['org_id'])
org_id=list(set(org_id))
filea.close()
for org in org_id:
with open(tpath+'mail_list.txt','r') as fileb:
b = fileb.readlines()
fileb.close()
for mail_list in b:
#print mail_list
mail_list=mail_list.replace('\n','')
mail_list=mail_list.replace('\r','')
mail_list=mail_list.split('\t')
if org==mail_list[0]:
count+=1
print mail_list[3]
to_list=mail_list[3].split(',')
to_list.extend(['xxx@xxx.com',111@222.com])
sub='xxx-xxx图书馆'+mail_list[1]+'的xx即将到期'
filepath=npath+'/'+org+'.txt'
file_name=org+'.txt'
with open(filepath,'r') as filec:
biaoge=filec.read()
filec.close()
neirong='<font color="#FF0000">'+mail_list[1]+'</font>以下xx'+'还有<font color="#FF0000"> %s</font> 天就要到期了,到期时间为:<font color="#FF0000">%s</font>请及时申请延期。<br/><br/>如续签或延期请找商务合同管理员索要最新的可发货报单,与客户沟通确定本次续购的报单,注意替换掉原订单中更新状况不佳或停止更新的报纸,以保证更好的客户体验。<br/><br/>'%(days,order['read_end_date'])+'<table border="1" cellspacing="0"><thead><tr><th class="col1"><strong>机构名称</strong></th><th class="col2"><strong>报纸名称</strong></th><th class="col3"><strong>到期时间</strong></th></tr></thead><tbody>'+biaoge+'</tbody></table>'
print to_list
if send_mail(to_list,sub,neirong):
print "发送成功"
else:
print "发送失败"
if count != len(org_id):
to_list=['wangjingfeng@founder.com']
sub='机构邮箱未找到,请及时补充,谢谢。'
neirong='机构邮箱未找到,请及时补充,谢谢。'
if send_mail(to_list,sub,neirong):
print "发送成功"
else:
print "发送失败"
else:
print "ok"

mysql_select(10)
mysql_select(30)
mysql_select(90)

maillist.txt 格式如下:

机构ID    机构名称    销售人员    收件人列表
xxx    xxx    xxx    w@xxx.com

QQ截图20160412125410


Whatever is worth doing is worth doing well.