class EmailMonitor: def __init__(self, logger): config = json.load(open("jobs/EmailMonitorJob/config.json", "r")) self.USERNAME = config.get("USERNAME", None) self.PASSWORD = config.get("PASSWORD", None) if not self.USERNAME or not self.PASSWORD: logger.error("can not find username and password") raise Exception("can not find username and password") self.isLogin = False self.logger = logger self.threadPool = ThreadPoolExecutor(max_workers=5) # 线程池 def _login(self): try: self.logger.debug("尝试登录邮箱") self.client = IMAPClient(HOST, PORT) loginInfo = self.client.login(self.USERNAME, self.PASSWORD) self.logger.debug("loginInfo: {0}".format(loginInfo)) self.logger.debug("邮箱登录成功") self.isLogin = True except Exception as e: self.isLogin = False self.logger.error("邮箱登录失败: " + str(e)) def start(self): # 先登录 if not self.isLogin: self._login() if not self.isLogin: return self.logger.warn("邮箱未登录") try: noopInfo = self.client.noop() # 发送心跳 self.logger.debug("noopInfo: {0}".format(noopInfo)) self.getEmails() except Exception as e: self.isLogin = False self.logger.error("".join(("获取邮件列表失败: ", str(e), "\n", traceback.format_exc()))) def getEmails(self): # 进入收件箱 self.client.select_folder("INBOX", False) # 获取未读邮件 unseens = self.client.search("UNSEEN", "gb2312") msgDict = self.client.fetch(unseens, ["BODY.PEEK[]"]) for msgid, message in msgDict.items(): # 邮件内容 msg = email.message_from_bytes(message[b"BODY[]"]) # 发送人 From = email.header.make_header(email.header.decode_header(msg["From"])) From = EMAILPATTERN.findall(str(From)) # print("From: ", From) # 邮件编码 encoding = msg.get_content_charset() if not encoding: encoding = [en for en in msg.get_charsets() if en] encoding = "gb2312" if not encoding else encoding[0] self.logger.debug("email encoding: {0}".format(encoding)) mainType = msg.get_content_maintype() mail_content = "" if mainType == "multipart": for part in msg.get_payload(): if part.get_content_maintype() == "text": mail_content += part.get_payload( decode=True).decode(encoding, errors="ignore") elif mainType == "text": mail_content = msg.get_payload( decode=True).decode(encoding, errors="ignore") # 标记邮件已读 self.client.set_flags(msgid, "SEEN") try: self.parseEmail(From, mail_content) except Exception as e: traceback.print_exc() self.logger.warn("parseEmail error: {0}".format(str(e))) def parseEmail(self, From, mail_content): # self.logger.debug("mail_content: {0}".format(mail_content)) self._parseTxtEmail(From, mail_content) self._parseMmsEmail(From, mail_content) self._parseCaseEmail(From, mail_content) def _parseTxtEmail(self, From, mail_content): results = TXTPATTERN.findall(mail_content) self.logger.debug("_parseTxtEmail results: %s" % results) # 发现控制命令 if results and len(results) >= 1: ver, eid, _, whichs = results[0] whichs = [w for w in whichs.split(",") if w != ""] self.logger.debug("ver:{0}, eid:{1},whichs:{2}".format(ver, eid, whichs)) self.logger.debug("email monitor :%s %s" % (eid, AutoReportGlobals.EmailIds.get(eid))) emailInfo = AutoReportGlobals.EmailIds.get(eid) if not emailInfo: def errorReply(eid, From): # 发送邮件 try: message = MIMEMultipart() subject = "日报控制-错误" message.attach(MIMEText("未找到该email id: {0}".format(eid))) sendEmail(subject, message, From) del AutoReportGlobals.EmailIds[eid] # 删除 except Exception as e: self.logger.error(str(e)) self.threadPool.submit(errorReply, eid, From) return self.logger.error("未找到该email id: {0}".format(eid)) subject = emailInfo.get("subject") # 获取主题 content = emailInfo.get("content") # peoples = emailInfo.get("formalSend") # 获取正式发送人 if ver == "文本发送": self.logger.debug("eid:{0}, subject:{1}, content:{2}, peoples:{3}, whichs:{4}".format(eid, subject, content, peoples, whichs)) def controlReply(subject, content, peoples, whichs, From): # 发送文本彩信 try: result = sendEmailText(peoples, subject, content) message = MIMEMultipart() subject = "日报控制-" + ("成功" if result == 1 else "失败") message.attach(MIMEText(str(result))) sendEmail(subject, message, From) del AutoReportGlobals.EmailIds[eid] # 删除 except Exception as e: self.logger.error(str(e)) return self.threadPool.submit(controlReply, subject, content, peoples, whichs, From) del AutoReportGlobals.EmailIds[eid] # 删除 def _parseMmsEmail(self, From, mail_content): results = MMSPATTERN.findall(mail_content) # print(mail_content) # print("_parseMmsEmail results: ", results) self.logger.debug("_parseMmsEmail results: %s" % results) # 发现控制命令 if results and len(results) >= 1: # print("results: ", results) # self.logger.info("results: {0}".format(repr(results))) ver, eid, _, whichs = results[0] whichs = [w for w in whichs.split(",") if w != ""] self.logger.debug("ver:{0}, eid:{1},whichs:{2}".format(ver, eid, whichs)) # import os # print(os.getpid()) # print("email monitor :", eid, AutoReportGlobals.EmailIds.get(eid), AutoReportGlobals) self.logger.debug("email monitor :%s %s" % (eid, AutoReportGlobals.EmailIds.get(eid))) # print(id(AutoReportGlobals.EmailIds)) emailInfo = AutoReportGlobals.EmailIds.get(eid) # print("ejob: ", AutoReportGlobals.EmailIds) if not emailInfo: def errorReply(eid, From): # 发送邮件 try: message = MIMEMultipart() subject = "日报控制-错误" message.attach(MIMEText("未找到该email id: {0}".format(eid))) sendEmail(subject, message, From) del AutoReportGlobals.EmailIds[eid] # 删除 except Exception as e: self.logger.error(str(e)) self.threadPool.submit(errorReply, eid, From) return self.logger.error("未找到该email id: {0}".format(eid)) subject = emailInfo.get("subject") # 获取主题 dataDir = emailInfo.get("dataDir") # 获取数据目录 peoples = emailInfo.get("formalSend") # 获取正式发送人 if ver == "日报发送": self.logger.debug("eid:{0}, subject:{1}, dataDir:{2}, peoples:{3}, whichs:{4}".format(eid, subject, dataDir, peoples, whichs)) def controlReply(subject, dataDir, peoples, whichs, From): # 发送彩信和邮件 try: result = sendMms(subject, dataDir, peoples, whichs, "") message = MIMEMultipart() subject = "日报控制-" + ("成功" if result == 1 else "失败") message.attach(MIMEText(str(result))) sendEmail(subject, message, From) del AutoReportGlobals.EmailIds[eid] # 删除 except Exception as e: self.logger.error(str(e)) return self.threadPool.submit(controlReply, subject, dataDir, peoples, whichs, From) del AutoReportGlobals.EmailIds[eid] # 删除 def _parseCaseEmail(self, From, mail_content): results = CASEPATTERN.search(mail_content) # 发现控制命令 if results: results = [text for text in results.groups() if text != None][1:] print("results:", results) # self.logger.info("results: {0}".format(repr(results))) ver = results[0] if ver == "绿网查看": code = results[1] print(ver, code) # self.logger.debug("%s" % results) def caseView(code, From): try: req = requests.get(DETAIL_URL.format(code), headers=BHEADERS, cookies=AutoReportGlobals.GreenCookie, timeout=60) if req.text.find("版权所有") > 0: # 需要重新登录 self.logger.warn("需要重新登录") login() # 重新登录 self.logger.debug("登录成功") req = requests.get(DETAIL_URL.format(code), headers=BHEADERS, cookies=AutoReportGlobals.GreenCookie, timeout=60) # print(req, req.text.find("版权所有")) if req.text.find("版权所有") > 0: # 需要重新登录 return self.logger.warn("又需要重新登录,放弃此操作") if not hasattr(self, "CaseStyle"): # ../../datas/jobs/EmailMonitorJob/ self.CaseStyle = open("jobs/EmailMonitorJob/CaseStyle.css", "rb").read().decode() + "</style>" html = req.text.replace('class="menu_title"', 'class="menu_title" style="display: none;"').replace("</style>", self.CaseStyle) # 替换样式表 self.logger.debug("email len: %s" % len(html)) # 发送邮件 message = MIMEMultipart() subject = "工单查看-%s" % code message.attach(MIMEText(html, _subtype="html", _charset="gb2312")) sendEmail(subject, message, From) self.logger.debug("工单查看邮件已发送") except Exception as e: self.logger.error(str(e)) # caseView(code, From) # 调试 self.threadPool.submit(caseView, code, From) self.logger.debug("绿网查看邮件后台发送中") if ver == "绿网回复": code, which, text = results[1:4] print(ver, code, which, text) # self.logger.debug("%s" % results) if ver == "绿网检索": num = results[1] texts = results[2].split(",") print(ver, num, texts)
class EmailMonitor: def __init__(self, logger): config = json.load(open("jobs/EmailMonitorJob/config.json", "r")) # print(config) self.USERNAME = config.get("USERNAME", None) self.PASSWORD = config.get("PASSWORD", None) # print(self.USERNAME) if not self.USERNAME or not self.PASSWORD: logger.error("can not find username and password") raise Exception("can not find username and password") self.isLogin = False self.logger = logger self.threadPool = ThreadPoolExecutor(max_workers=5) # 线程池 def _login(self): try: self.logger.debug("尝试登录邮箱") self.client = IMAPClient(HOST, PORT) loginInfo = self.client.login(self.USERNAME, self.PASSWORD) self.logger.debug("loginInfo: {0}".format(loginInfo)) self.logger.debug("邮箱登录成功") self.isLogin = True except Exception as e: self.isLogin = False self.logger.error("邮箱登录失败: " + str(e)) def start(self): # 先登录 if not self.isLogin: self._login() if not self.isLogin: return self.logger.warn("邮箱未登录") try: noopInfo = self.client.noop() # 发送心跳 self.logger.debug("noopInfo: {0}".format(noopInfo)) self.getEmails() except Exception as e: self.isLogin = False self.logger.error("".join( ("获取邮件列表失败: ", str(e), "\n", traceback.format_exc()))) def getEmails(self): # 进入收件箱 self.client.select_folder("INBOX", False) # 获取未读邮件 unseens = self.client.search("UNSEEN", "gb2312") msgDict = self.client.fetch(unseens, ["BODY.PEEK[]"]) for msgid, message in msgDict.items(): # 邮件内容 msg = email.message_from_bytes(message[b"BODY[]"]) # 发送人 From = email.header.make_header( email.header.decode_header(msg["From"])) From = EMAILPATTERN.findall(str(From)) # print("From: ", From) # 邮件编码 encoding = msg.get_content_charset() if not encoding: encoding = [en for en in msg.get_charsets() if en] encoding = "gb2312" if not encoding else encoding[0] self.logger.debug("email encoding: {0}".format(encoding)) mainType = msg.get_content_maintype() mail_content = "" if mainType == "multipart": for part in msg.get_payload(): if part.get_content_maintype() == "text": mail_content += part.get_payload(decode=True).decode( encoding, errors="ignore") elif mainType == "text": mail_content = msg.get_payload(decode=True).decode( encoding, errors="ignore") bs = BeautifulSoup(mail_content, "lxml") mail_content = "" for _content in bs.strings: # 提炼所有文字(包含空格) mail_content += _content # 切割 mail_content = mail_content[0:mail_content.find("#说明")] # 标记邮件已读 self.client.set_flags(msgid, "SEEN") try: self.parseEmail(From, mail_content) except Exception as e: traceback.print_exc() self.logger.warn("parseEmail error: {0}".format(str(e))) def parseEmail(self, From, mail_content): # self.logger.debug("mail_content: {0}".format(mail_content)) results = PATTERN.findall(mail_content) if not results: return self.logger.warn("邮件内容没有匹配到结果") results = results[0] ver, other = results[0], results[1:] other = re.split(",|,", other) # 正则分割 eid, whichs = other[0], other[1:] if ver == "M": self.doMmsMail(From, eid, whichs) # 处理图片彩信 elif ver == "T": self.doTextEmail(From, eid) # 处理文字彩信 elif ver == "L": self.doGreenCase(From, eid) # 绿网查看 def doTextEmail(self, From, eid): self.logger.debug("doTextEmail From:%s eid: %s" % (From, eid)) if not From or not eid: return self.logger.warn("发送人或者ID未找到") self.logger.debug("email monitor :%s %s" % (eid, AutoReportGlobals.EmailIds.get(eid))) emailInfo = AutoReportGlobals.EmailIds.get(eid) if not emailInfo: def errorReply(eid, From): # 发送邮件 try: message = MIMEMultipart() subject = "控制-错误" message.attach(MIMEText("未找到该email id: {0}".format(eid))) sendEmail(subject, message, From) del AutoReportGlobals.EmailIds[eid] # 删除 except Exception as e: self.logger.error(str(e)) self.threadPool.submit(errorReply, eid, From) return self.logger.error("未找到该email id: {0}".format(eid)) subject = emailInfo.get("subject") # 获取主题 content = emailInfo.get("content") # peoples = emailInfo.get("formalSend") # 获取正式发送人 def controlReply(subject, content, peoples, From): # 发送文本彩信 try: result = sendTextMms(subject, content, peoples) message = MIMEMultipart() subject = "控制-" + ("成功" if result == 1 else "失败") message.attach(MIMEText(str(result))) sendEmail(subject, message, From) del AutoReportGlobals.EmailIds[eid] # 删除 except Exception as e: self.logger.error(str(e)) del AutoReportGlobals.EmailIds[eid] # 删除 return self.threadPool.submit(controlReply, subject, content, peoples, From) def doMmsMail(self, From, eid, whichs): self.logger.debug("doMmsMail From:%s, eid: %s, whichs: %s" % (From, eid, whichs)) if not From or not eid: return self.logger.warn("发送人或者ID未找到") whichs = [w for w in whichs if w != ""] # 去掉空 # import os # print(os.getpid()) # print("email monitor :", eid, AutoReportGlobals.EmailIds.get(eid), AutoReportGlobals) self.logger.debug("email monitor :%s %s" % (eid, AutoReportGlobals.EmailIds.get(eid))) # print(id(AutoReportGlobals.EmailIds)) emailInfo = AutoReportGlobals.EmailIds.get(eid) # print("ejob: ", AutoReportGlobals.EmailIds) if not emailInfo: def errorReply(eid, From): # 发送邮件 try: message = MIMEMultipart() subject = "日报控制-错误" message.attach(MIMEText("未找到该email id: {0}".format(eid))) sendEmail(subject, message, From) del AutoReportGlobals.EmailIds[eid] # 删除 except Exception as e: self.logger.error(str(e)) self.threadPool.submit(errorReply, eid, From) return self.logger.error("未找到该email id: {0}".format(eid)) subject = emailInfo.get("subject") # 获取主题 dataDir = emailInfo.get("dataDir") # 获取数据目录 peoples = emailInfo.get("formalSend") # 获取正式发送人 self.logger.debug( "eid:{0}, subject:{1}, dataDir:{2}, peoples:{3}, whichs:{4}". format(eid, subject, dataDir, peoples, whichs)) def controlReply(subject, dataDir, peoples, whichs, From): # 发送彩信和邮件 try: result = sendMms(subject, dataDir, peoples, whichs, "") message = MIMEMultipart() subject = "日报控制-" + ("成功" if result == 1 else "失败") message.attach(MIMEText(str(result))) sendEmail(subject, message, From) del AutoReportGlobals.EmailIds[eid] # 删除 except Exception as e: self.logger.error(str(e)) del AutoReportGlobals.EmailIds[eid] # 删除 return self.threadPool.submit(controlReply, subject, dataDir, peoples, whichs, From) def doGreenCase(self, From, eid): self.logger.debug("doGreenCase From:%s eid: %s" % (From, eid)) if not From or not eid: return self.logger.warn("发送人或者ID未找到") def caseView(From, eid): try: req = requests.get(DETAIL_URL.format(eid), headers=BHEADERS, cookies=AutoReportGlobals.GreenCookie, timeout=60) if req.text.find("版权所有") > 0: # 需要重新登录 self.logger.warn("需要重新登录") login() # 重新登录 self.logger.debug("登录成功") req = requests.get(DETAIL_URL.format(eid), headers=BHEADERS, cookies=AutoReportGlobals.GreenCookie, timeout=60) # print(req, req.text.find("版权所有")) if req.text.find("版权所有") > 0: # 需要重新登录 return self.logger.warn("又需要重新登录,放弃此操作") if not hasattr( self, "CaseStyle"): # ../../datas/jobs/EmailMonitorJob/ self.CaseStyle = open("jobs/EmailMonitorJob/CaseStyle.css", "rb").read().decode() + "</style>" html = req.text.replace( 'class="menu_title"', 'class="menu_title" style="display: none;"').replace( "</style>", self.CaseStyle) # 替换样式表 self.logger.debug("email len: %s" % len(html)) # 发送邮件 message = MIMEMultipart() subject = "工单查看-%s" % eid message.attach( MIMEText(html, _subtype="html", _charset="gb2312")) sendEmail(subject, message, From) self.logger.debug("工单查看邮件已发送") except Exception as e: self.logger.error(str(e)) # caseView(code, From) # 调试 self.threadPool.submit(caseView, From, eid) self.logger.debug("绿网查看邮件后台发送中")
elif request["type"] == "fetch-email": try: email = get_email(request["id"], request["format"]) response = dict(success=True, type="fetch-email", email=email, format=request["format"]) except Exception as error: response = dict(success=False, type="fetch-email", error=str(error)) elif request["type"] == "download-attachments": try: fetch = imap_client.fetch([request["id"]], ["BODY[]"]) attachments = download_attachments(request["dir"], request["id"], fetch.popitem()[1][b"BODY[]"]) response = dict(success=True, type="download-attachments", attachments=attachments) except Exception as error: response = dict(success=False, type="download-attachments", error=str(error)) elif request["type"] == "select-folder": try: folder = request["folder"] seq = imap_client.select_folder(folder)[b"UIDNEXT"] emails = get_emails(seq, request["chunk-size"])
def imapPage(): #folder = request.args.get('folder','') #if (folder == ''): # folder="All Mail" email = request.cookies.get('email') accesskey = request.cookies.get('ak') if email != "" and accesskey != "": try: server = IMAPClient('imap.gmail.com', use_uid=True, ssl=True) server.oauth2_login(email, accesskey) except Exception, e: print "oauth2_login ERROR??" print e.__doc__ print e.message return "oauth2_login Error. Remove cookies and try again" #GET FOLDERS folders = server.list_folders() stri=""; for folderx in folders: print folderx if 'SPAM' not in folderx[2].upper() and 'JUNK' not in folderx[2].upper() and folderx[2].upper()!='[GMAIL]': print "entered" folder=folderx[2] stri = stri + "\n"+str(folderx[0])+'|'+str(folderx[1])+'|'+str(folderx[2]); select_info = 0 try: select_info = server.select_folder(folder, readonly=True) except Exception, e: print "MAILBOX ERROR??" print e.__doc__ print e.message continue #print select_info #messages = server.search(['SINCE 05-Feb-2014 NOT FROM \"facebook\"']) messages = server.search(['SINCE 17-Feb-2014']) #print "-" #print messages #print "-" response = server.fetch(messages, ['X-GM-MSGID', 'X-GM-THRID', 'FLAGS', 'INTERNALDATE', 'ENVELOPE']) for msgid, data in response.iteritems(): #print "===========" #print data['FLAGS'] #print data['INTERNALDATE'] #print data['ENVELOPE'] #print data['BODY[HEADER]'] ''' The fields of the envelope structure are in the following order: date, subject, from, sender, reply-to, to, cc, bcc, in-reply-to, and message-id. The date, subject, in-reply-to, and message-id fields are strings. The from, sender, reply-to, to, cc, and bcc fields are parenthesized lists of address structures. ''' ## #Making proper data structure for Envelope ## env = {} env['date']= data['ENVELOPE'][0] env['subject']= data['ENVELOPE'][1] env['in-reply-to']= data['ENVELOPE'][8] env['message-id']= data['ENVELOPE'][9] env['from']= ParseAddressStructure(data['ENVELOPE'][2]) env['sender']= ParseAddressStructure(data['ENVELOPE'][3]) env['reply-to']= ParseAddressStructure(data['ENVELOPE'][4]) env['to']= ParseAddressStructure(data['ENVELOPE'][5]) env['cc']= ParseAddressStructure(data['ENVELOPE'][6]) env['bcc']= ParseAddressStructure(data['ENVELOPE'][7]) em = Email(email, data['X-GM-MSGID'],data['X-GM-THRID'],env['subject'], data['INTERNALDATE'], env['in-reply-to']) try: db.session.add(em) db.session.flush() except IntegrityError: db.session.rollback() #print "Duplicate Ignored\n" addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'from', env['from'] ) addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'sender', env['sender'] ) addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'reply-to', env['reply-to'] ) addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'to', env['to'] ) addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'cc', env['cc'] ) addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'bcc', env['bcc'] ) # Or for parsing headers in a string, use: #headers = Parser().parsestr(data['BODY[HEADER]']) # Now the header items can be accessed as a dictionary: #print 'To: %s' % headers['to'] #print 'From: %s' % headers['from'] #print 'Subject: %s' % headers['Subject'] #print 'Date: %s' % headers['date'] #print "+++++++" db.session.commit()