# coding=utf-8 import requests, ConfigParser, os, datetime, re, smtplib from lxml import etree from bs4 import BeautifulSoup from email.mime.text import MIMEText from email.header import Header from email.utils import parseaddr, formataddr def get_conf(): conf_file = ConfigParser.ConfigParser() conf_file.read(os.path.join(os.getcwd(), 'sendOnlinePlan.ini')) def getContent(confluenceLoginUrl, confluenceOnlinePlanUrl, paramLogin): s = requests.session() r_login = s.post(confluenceLoginUrl, paramLogin) r_OnlinePlan = s.get(confluenceOnlinePlanUrl) pageHtml = r_OnlinePlan.content.decode('utf-8') # print pageHtml return pageHtml def genMSGtext(pageHtml): r_OnlinePlan_soup = BeautifulSoup(pageHTML, 'lxml' ) # print(r_OnlinePlan_soup) r_OnlinePlan_soup_table = r_OnlinePlan_soup.find_all('table') # print r_OnlinePlan_soup_table table1_soup = BeautifulSoup(str(r_OnlinePlan_soup_table[0]), 'lxml') tr = table1_soup.find_all('tr') #设置邮件格式 table_css = ''' <html> <head> <style type="text/css"> table, td, tr { border:1px solid grey; border-collapse: collapse; font-size:12px; } </style> </head> <body> hi, all<br> 以下是家政业务线上线计划,请阅:<br> <table class="relative-table wrapped confluenceTable" ;"> <colgroup> <col style="width: 30px;"/> <col style="width: 40px;"/> <col style="width: 40px;"/> <col style="width: 40px;"/> <col style="width: 300px;"/> <col style="width: 60px;"/> <col style="width: 150px;"/> <col style="width: 70px;"/> <col style="width: 70px;"/> <col style="width: 70px;"/> <col style="width: 40px;"/> <col style="width: 40px;"/> <col style="width: 40px;"/> <col style="width: 40px;"/> <col style="width: 40px;"/> <col style="width: 75px;"/> <col style="width: 75px;"/> <col style="width: 75px;"/> <col style="width: 40px;"/> <col style="width: 40px;"/> <col style="width: 40px;"/> <col style="width: 40px;"/> <col style="width: 40px;"/> <col style="width: 30px;"/> </colgroup> <tbody> ''' msg_content = table_css + str(tr[0]) #时间格式化,将20170701转化为2017-7-7 #currentTime = '20160919' currentTime = datetime.datetime.now() # print(currentTime) year = currentTime.strftime('%Y') # print(year) month = currentTime.strftime('%m') # print(month) day = currentTime.strftime('%d') # print(day) today = year+'-'+str(int(month))+'-'+str(int(day)) #上线计划的个数 planNum = 0 for r in tr: if r.find(text=today) is not None: print r planNum = planNum + 1 msg_content = msg_content + str(r) if planNum > 0: msg_content = msg_content + '</tbody></table></body></html>' else: msg_content = None return msg_content def sendMail(msg_content): #邮件发件人格式化 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)) from_addr = 'lina_01@juran.com.cn' #填写发件人邮箱 mail_account = 'lina_01@juran.com.cn' #填写发件人邮箱地址 mail_password = 'abc2266' #填写发件人邮箱密码 to_addr = ['lina_01@juran.com.cn'] #收件人邮箱地址 currentDate = datetime.datetime.now().strftime('%Y年%m月%d日') #更改为各自具体业务线名称 mail_Subject = '业务线' + currentDate + '上线计划' #采用MIMEText使得邮件发送内容兼容HTML格式 msg = MIMEText(msg_content, 'html', 'utf-8') msg['From'] = _format_addr(from_addr) msg['To'] = _format_addr(to_addr) msg['Subject'] = _format_addr(mail_Subject) #建议此处采用企业邮箱的smtp,普通用户的服务器可能认为不安全,会报授权码的错误 smtp_server = 'smtp.juran.com.cn' #server = smtplib.SMTP_SSL(smtp_server, smtp_port) #此方式在服务器上不支持!!!换成以下两条语句就ok server = smtplib.SMTP(smtp_server,25) server.starttls() server.set_debuglevel(1) server.login(mail_account, mail_password) server.sendmail(from_addr, to_addr, msg.as_string()) server.quit() if __name__ == '__main__': #此处更改上线计划地址 lrOnlinePlanUrl = 'http://wiki-dev.shejijia.com:8090/pages/viewpage.action?pageId=6063917' confluenceLoginUrl = 'http://wiki-dev.shejijia.com:8090/login.action' #此处填写wiki的登录帐号密码 paramLogin = {'os_username': 'test', 'os_password': '1122'} #登录wiki, 并获取到上线计划的html pageHTML = getContent(confluenceLoginUrl, lrOnlinePlanUrl, paramLogin) #获取表格内容并生成邮件内容 msg_content = None try: msg_content = genMSGtext(pageHTML) except : print '没有发现上线计划内容~' #发送邮件 if msg_content is not None: sendMail(msg_content) else: print '今日暂无上线计划~'
上线计划定时发送
于 2017-07-06 18:17:36 首次发布