def on_restoreEmail_clicked(self): # Yes:0 Cancel:1 isRestore = QMessageBox.information(self, '恢复邮件', u'确定要恢复该封邮件?', 'Yes', 'Cancel') if not isRestore: # 获取主题名 my_subject = self.contEmailSubject.text() self.receive = GetJsonInfo(self.receiveJsonName) self.delete = GetJsonInfo(self.deleteJsonName) temp = {my_subject: self.delete[my_subject]} print(temp) # 将删除邮件恢复至receive.json文件中 self.receive.update(temp) # self.isReceived.update(temp) # 将邮件从delete.json中去除 self.delete.pop(my_subject) self.isDeleted.pop(my_subject) # 数据回写 SaveJsonInfo(self.receiveJsonName, self.receive) SaveJsonInfo(self.deleteJsonName, self.delete) self.deleteList.takeItem(self.deleteList.currentRow()) self.addQList(self.receive, 'emaillist') self.contEmail.setText('') self.contEmailTime.setText('') self.contEmailSubject.setText('') self.contName.setText('') self.emailShow.setUrl(QtCore.QUrl("qrc:/souce/index.html")) self.mainForward.hide() self.delEmail.hide()
def on_mainSearch_clicked(self): if self.searchMode.currentText() == '请选择': my_alert = QMessageBox.warning(self, '搜索失败', u'请选择一种搜索模式') else: keyword = self.searchlineEdit.text() # 获取搜索关键字 if keyword: self.userInfo = GetJsonInfo('conf.json') self.receive = GetJsonInfo(self.receiveJsonName) self.emaillist.hide() self.sentList.hide() self.deleteList.hide() self.draftList.hide() self.searchList.show() begin = time.time() test = Search() test.run(self.userInfo, self.searchMode.currentText(), keyword) files = test.getResult() while files == None: files = test.getResult() print("2", files) self.searchList.clear() if len(files) > 0: self.addQList(files, 'searchList') else: self.addQList( { "搜索结果为空": { "date": "", "subject": "搜索结果为空", "name": "" } }, 'searchList') print("耗时啥都不用2:", time.time() - begin) else: my_alert = QMessageBox.warning(self, '搜索失败', u'搜索内容不能为空!')
def on_sync_clicked(self): self.receive = GetJsonInfo(self.receiveJsonName) self.contacts = GetJsonInfo(self.contactsJsonName) for item in self.receive: if self.receive[item]['fromAddr'] not in self.contacts: emailAddr = self.receive[item]['fromAddr'] temp = { emailAddr: { 'fromAddr': emailAddr, 'name': self.receive[item]['name'] } } self.contacts.update(temp) SaveJsonInfo(self.contactsJsonName, self.contacts) self.contactsList.clear() for x in self.contacts: if x != self.emailInfo['email']: item = QtGui.QListWidgetItem() item.setTextAlignment(QtCore.Qt.AlignLeading | QtCore.Qt.AlignVCenter) icon3 = QtGui.QIcon() num = self.generateNum(x) icon3.addPixmap(QtGui.QPixmap(":/avatar/Avatars/%d.jpg" % num), QtGui.QIcon.Normal, QtGui.QIcon.Off) item.setIcon(icon3) my_contact = self.contacts[x]['name'] + '\n' + self.contacts[ x]['fromAddr'] item.setText(my_contact) self.contactsList.addItem(item) QMessageBox.information(self, '同步联系人', u'同步成功', 'ok')
def on_delEmail_clicked(self): isDel = QMessageBox.information(self, '删除邮件', u'确定要删除该封邮件?', 'Yes', 'Cancel') if self.item_enable_delete: if not isDel: # 获取主题名 my_subject = self.contEmailSubject.text() self.receive = GetJsonInfo(self.receiveJsonName) self.delete = GetJsonInfo(self.deleteJsonName) # 将删除邮件存至delete.json文件中 temp = {my_subject: self.receive[my_subject]} print(temp) self.delete.update(temp) # self.isDeleted.update(temp) # 将邮件从receive.json中去除 self.receive.pop(my_subject) self.isReceived.pop(my_subject) # 数据回写 SaveJsonInfo(self.receiveJsonName, self.receive) SaveJsonInfo(self.deleteJsonName, self.delete) self.contEmail.setText('') self.contEmailTime.setText('') self.contEmailSubject.setText('') self.contName.setText('') self.emailShow.setUrl(QtCore.QUrl("qrc:/souce/index.html")) self.emaillist.takeItem(self.emaillist.currentRow()) self.item_enable_delete = False self.mainForward.hide() self.delEmail.hide() self.addQList(self.delete, 'deleteList')
def on_mainlogin_clicked(self): self.hide() my_login = Login() my_login.exec_() self.emailInfo = GetJsonInfo('conf.json') if self.emailInfo['status'] == 1: self.mainUserName.setText(self.emailInfo['email']) self.mainlogin.setText('切换') self.emaillist.clear() self.login = 1 self.show() # 获取当前文件的绝对路径 abDir = os.path.abspath(os.path.join( os.path.dirname(__file__))).replace('\\', '/') dir = "%s/data/%s/" % (abDir, self.emailInfo['email']) self.receiveJsonName = dir + "receive.json" self.sendJsonName = dir + "send.json" self.deleteJsonName = dir + "delete.json" self.draftJsonName = dir + "draft.json" self.isReceived = {} self.isDeleted = {} self.isDraft = {} self.isSent = {} self.addQList(GetJsonInfo(self.receiveJsonName), 'emaillist') num = self.generateNum(self.emailInfo['email']) self.headlogo.setStyleSheet(''' background-color: rgb(242, 242, 242); border-radius:40px; border-image: url(:/avatar/Avatars/%d.jpg); ''' % num)
def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog) # 邮件数量 self.index = 0 self.page = 0 # 判断是否已经的登录邮箱,若已经登录才能进行后续操作 self.login = 0 # 登录上次账号 try: start = time.time() self.emailInfo = GetJsonInfo('conf.json') server = smtplib.SMTP_SSL(self.emailInfo["smtp_server"], 465) server.set_debuglevel(1) print('\n***************************************\n\n') server.login(self.emailInfo["email"], self.emailInfo["pwd"]) self.emailInfo["status"] = 1 SaveJsonInfo('conf.json', self.emailInfo) end = time.time() print('耗时:' + str(end - start)) self.mainUserName.setText(self.emailInfo['email']) self.mainlogin.setText('切换账号') self.login = 1 except Exception as e: print(e) self.emailInfo = { "pop3_server": "", "email": "", "pwd": "", "smtp_server": "", "status": 0 } SaveJsonInfo('conf.json', self.emailInfo) self.emailInfo = GetJsonInfo('conf.json') self.attachList.hide() self.mainForward.hide() self.delEmail.hide() self.mainReply.hide() self.mainAttach.hide() # 绑定emailList self.connect(self.emaillist, SIGNAL('itemClicked(QListWidgetItem *)'), self.emailItemClicked) # 绑定attachList self.connect(self.attachList, SIGNAL('itemClicked(QListWidgetItem *)'), self.attachItemClicked) # 绑定searchList self.connect(self.searchList, SIGNAL('itemClicked(QListWidgetItem *)'), self.searchItemClicked)
def on_delContact_clicked(self): # 获取邮箱地址 contEmail = self.contEmail.text() if contEmail: isDel = QMessageBox.information(self, '删除联系人', u'确定要删除该联系人?', 'Yes', 'Cancel') if not isDel: # 获取邮箱地址 contEmail = self.contEmail.text() # 获取联系人数据 self.contacts = GetJsonInfo(self.contactsJsonName) self.contacts.pop(contEmail) # 联系人数据回写 SaveJsonInfo(self.contactsJsonName, self.contacts) self.contName.setText('') self.contEmail.setText('') self.contLogo.setStyleSheet("") self.contRemarkName.setText('') self.contPhone.setText('') self.contQQ.setText('') self.contWechat.setText('') self.contactsList.takeItem(self.contactsList.currentRow()) else: QMessageBox.information(self, '删除联系人', u'请先选择要删除的联系人', 'ok')
def on_login_clicked(self): # 登录 self.run() self.emailInfo = GetJsonInfo('conf.json') if self.emailInfo['status'] == 0: alert = QMessageBox.warning(self, '登录失败', u'登录失败,请检查配置是否正确!') self.resize(500, 370) else: # 创建一个以邮件命名的文件夹,用于区分用户信息 dir = '/data/%s' % (self.emailInfo['email']) # 获取当前文件的绝对路径 abDir = os.path.abspath(os.path.join( os.path.dirname(__file__))).replace('\\', '/') dir = abDir + dir if not os.path.exists(dir): os.mkdir(dir) # 创建用户邮件信息记录文件 files = ['receive', 'send', 'delete', 'draft', 'contacts'] for file in files: file = "%s/%s.json" % (dir, file) SaveJsonInfo(file, {}) os.mkdir(dir + '/receive') os.mkdir(dir + '/send') os.mkdir(dir + '/draft') print("文件夹创建成功:%s" % dir) self.close()
def __init__(self, parent=None,isForwad=False,ForwardInfo=None,url=None,\ isReply=False,replyInfo=None): super(WriteEmailDialog, self).__init__(parent) self.setupUi(self) self.emailInfo = GetJsonInfo('conf.json') self.email = SendMail(From = self.emailInfo['email'], pwd = self.emailInfo['pwd'], smtp_server = self.emailInfo['smtp_server']) self.isForwad = isForwad self.isReply = isReply if self.isForwad: self.emailText = GetEmailText(url) self.formatText = '''\n\n\n\n- 发送自XYZ邮箱 -\n-------- 转发的邮件 --------\n发件人: "%s" %s\n日期: %s\n主题: %s\n'''\ %(ForwardInfo['email'], ForwardInfo['name'], ForwardInfo['time'], ForwardInfo['subject']) self.emailContent.setPlainText(self.formatText) # 将转发信息格式化 str1 = self.formatText.split('-\n') str2 = str1[2].split('\n') str3 = (str1[0]+'-',str1[1]+'-',str2[0],str2[1],str2[2]) self.formatText = '<div>%s<br>%s<br>%s<br>%s<br>%s<br><div>\ </div><div style="clear:both;"></div>'% str3 if self.isReply: self.receiverEdit.setText(replyInfo['reply_addr']) self.subjectEdit.setText(replyInfo['reply_subject'])
def emailItemClicked(self): try: self.currentFolder = 'receive' self.item_enable_delete = True # 点击一个元素,可删除 my_receive = GetJsonInfo(self.receiveJsonName) my_currentItem = self.emaillist.currentItem() my_text = my_currentItem.text().split('主题:')[1].split('\n')[0] url = 'file:///' + os.path.abspath( os.path.join(os.path.dirname(__file__)) ) + r'/data/%s/%s.html' % (self.emailInfo['email'], my_text) url = url.replace('\\', '/') print(url) num = self.generateNum(my_currentItem.text().split('联系人:')[1]) self.contLogo.setStyleSheet( "border-image: url(:/avatar/Avatars/%d.jpg);" % num) self.contName.setText(my_receive[my_text]['name']) self.contEmail.setText(my_receive[my_text]['fromAddr']) self.contEmailTime.setText(my_receive[my_text]['date']) self.contEmailSubject.setText(my_text) self.mainForward.show() self.delEmail.show() self.mainReply.show() self.mainAttach.show() self.emailShow.setUrl(QtCore.QUrl(url)) self.attachList.hide() except Exception as e: print(str(e))
def on_contSave_clicked(self): email = self.contEmail.text() name = self.contName.text() if email and name: remark = self.contRemarkName.text() phone = self.contPhone.text() qq = self.contQQ.text() weChat = self.contWechat.text() self.contacts = GetJsonInfo(self.contactsJsonName) temp = { email: { 'name': name, 'fromAddr': email, 'remark': remark, 'phone': phone, 'qq': qq, 'weChat': weChat } } self.contacts.update(temp) SaveJsonInfo(self.contactsJsonName, self.contacts) self.createAlert.hide() self.must1.hide() self.must2.hide() QMessageBox.information(self, '保存联系人', u'保存成功', 'ok') else: QMessageBox.information(self, '保存联系人', u'用户名和邮箱不能为空!', 'ok')
def searchEmailAddr(self, email): contacts = GetJsonInfo(self.receiveJsonName) files = {} temp = email.lower() for item in contacts: if temp in contacts[item]['fromAddr'].lower(): files[item] = contacts[item] return files
def searchName(self, name): contacts = GetJsonInfo('contacts.json') files = {} temp = name.lower() for item in contacts: if temp in contacts[item]['name'].lower(): files[item] = contacts[item] return files
def searchDate(self, date): contacts = GetJsonInfo(self.receiveJsonName) files = {} temp = date.lower() for item in contacts: if temp in contacts[item]['date'].lower(): files[item] = contacts[item] return files
def __init__(self, parent=None): super(contacts, self).__init__(parent) self.setupUi(self) self.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog) self.emailInfo = GetJsonInfo('conf.json') self.contacts = GetJsonInfo('contacts.json') self.contUserMail.setText(self.emailInfo["email"]) # 绑定QListWidget self.connect(self.contactsList, SIGNAL('itemClicked(QListWidgetItem *)'), self.itemClicked) for item in self.contacts: my_contact = self.contacts[item]['name'] + '\n' + self.contacts[ item]['fromAddr'] self.contactsList.addItem(my_contact)
def run(self): # 获取登录信息 self.emailInfo["email"] = self.loginmail.text() # 密码加密 password = self.loginpwd.text() self.emailInfo["pwd"] = encrypt(password) smtp_server = self.loginsmtp.text() pop3_server = self.loginpop.text() # 使用上次的服务器地址 if smtp_server and pop3_server: self.emailInfo["smtp_server"] = smtp_server self.emailInfo["pop3_server"] = pop3_server self.emailInfo["status"] = 0 SaveJsonInfo('conf.json', self.emailInfo) # 若服务器地址为空,则自动获取 else: server = self.loginmail.text().split('@')[1].split('.com')[0] if server == 'foxmail': server = 'qq' smtp_server = 'smtp.%s.com' % server pop3_server = 'pop.%s.com' % server if 'hust' in server: smtp_server = 'mail.hust.edu.cn' pop3_server = 'mail.hust.edu.cn' self.emailInfo["smtp_server"] = smtp_server self.emailInfo["pop3_server"] = pop3_server self.emailInfo["status"] = 0 SaveJsonInfo('conf.json', self.emailInfo) self.loginsmtp.setText(smtp_server) self.loginpop.setText(pop3_server) # 开始登陆 try: start = time.time() self.emailInfo = GetJsonInfo('conf.json') server = smtplib.SMTP_SSL(smtp_server, 465) server.set_debuglevel(1) print('\n***************************************\n\n') # 密码解密 password = decrypt(self.emailInfo['pwd']) server.login(self.emailInfo["email"], password) self.emailInfo["status"] = 1 SaveJsonInfo('conf.json', self.emailInfo) end = time.time() print('耗时:' + str(end - start)) except Exception as e: print(type(e)) print(e) self.emailInfo["status"] = 0 SaveJsonInfo('conf.json', self.emailInfo)
def on_mainlogin_clicked(self): self.hide() my_login = Login() my_login.exec_() self.emailInfo = GetJsonInfo('conf.json') if self.emailInfo['status'] == 1: self.mainUserName.setText(self.emailInfo['email']) self.mainlogin.setText('切换账号') self.emaillist.clear() self.login = 1 self.show()
def on_draftBox_clicked(self): if self.login == 0: my_alert = QMessageBox.warning(self, '操作失败', u'请先登录您的账号!') else: self.emaillist.hide() self.searchList.hide() self.sentList.hide() self.deleteList.hide() self.delEmail.hide() self.restoreEmail.hide() draftJson = GetJsonInfo(self.draftJsonName) self.addQList(draftJson, 'draftList') self.draftList.show()
def searchSubject(self, subject): dir = 'data/%s' % (self.userInfo['email']) contacts = GetJsonInfo(self.receiveJsonName) files = {} subject = subject.lower() if os.path.exists(dir): for file in os.listdir(dir): if os.path.isfile(os.path.join(dir, file)): if subject in file.lower(): file = file.replace('.html', '') if file in contacts: files[file] = contacts[file] return files
def on_mainSearch_clicked(self): if self.searchMode.currentText() == '请选择': my_alert = QMessageBox.warning(self, '搜索失败', u'请选择一种搜索模式') else: keyword = self.searchlineEdit.text() # 获取搜索关键字 if keyword: self.userInfo = GetJsonInfo('conf.json') self.contacts = GetJsonInfo('contacts.json') self.emaillist.hide() self.sendedList.hide() self.searchList.show() begin = time.time() test = Search() test.run(self.userInfo, self.searchMode.currentText(), keyword) files = test.getResult() while files == None: files = test.getResult() print("2", files) self.searchList.clear() self.addQList(files, 'searchList') print("耗时啥都不用2:", time.time() - begin) else: my_alert = QMessageBox.warning(self, '搜索失败', u'搜索内容不能为空!')
def __init__(self, parent=None): super(Login, self).__init__(parent) self.setupUi(self) self.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog) self.emailInfo = GetJsonInfo('conf.json') # 初始化 if self.emailInfo["email"] and self.emailInfo["pwd"]: email = self.emailInfo["email"] pwd = self.emailInfo["pwd"] self.loginmail.setText(email) self.loginpwd.setText(pwd) self.loginsmtp.setText(self.emailInfo['smtp_server']) self.loginpop.setText(self.emailInfo['pop3_server'])
def searchEmailContent(self, content): content = re.compile(content) dir = 'data/%s' % (self.userInfo['email']) contacts = GetJsonInfo('contacts.json') abDir = os.path.abspath(os.path.join( os.path.dirname(__file__))).replace('\\', '/') + "/%s" % dir files = {} if os.path.exists(dir): for file in os.listdir(dir): if file[-4:] == 'html': fileDir = "%s/%s" % (abDir, file) with open(fileDir, 'rb') as f: emailContent = f.read().decode('utf-8') if len(content.findall(emailContent)) > 0: files[file] = contacts[file] return files
def searchEmailContent(self, content): contacts = GetJsonInfo(self.receiveJsonName) files = {} # 遍历邮件文件夹 if os.path.exists(self.dir): for file in os.listdir(self.dir): if file[-4:] == 'html': fileDir = "%s/%s" % (self.dir, file) with open(fileDir, 'rb') as f: emailContent = f.read().decode('utf-8') if content in emailContent: subject = file.replace('.html', '') files[subject] = contacts[subject] return files
def on_delEmail_clicked(self): isDel = QMessageBox.information(self, '删除邮件', u'确定要删除该封邮件?', 'Yes', 'Cancel') if self.item_enable_delete: if not isDel: my_subject = self.contEmailSubject.text() self.contacts = GetJsonInfo('contacts.json') self.contEmail.setText('') self.contEmailTime.setText('') self.contEmailSubject.setText('') self.contName.setText('') self.emailShow.setUrl(QtCore.QUrl("qrc:/souce/index.html")) self.contacts.pop(my_subject) self.emaillist.takeItem(self.emaillist.currentRow()) self.item_enable_delete = False self.mainForward.hide() self.delEmail.hide()
def addQListWidgetItem(self): dir = 'data/%s' % self.emailInfo['email'] files = [] for file in os.listdir(dir): print(file) if os.path.isfile(os.path.join(dir, file)): files.append(file) print('****************************\n\n') my_contacts = GetJsonInfo('contacts.json') for subject in my_contacts: filename = subject + '.html' if filename in files: abstractContent = my_contacts[subject][ 'date'] + '\n' + subject + '\n' + my_contacts[subject][ 'name'] self.emaillist.addItem(abstractContent)
def on_login_clicked(self): # 登录 self.run() self.emailInfo = GetJsonInfo('conf.json') if self.emailInfo['status'] == 0: alert = QMessageBox.warning(self, '登录失败', u'登录失败,请检查配置是否正确!') self.resize(500, 370) else: # 创建一个以邮件命名的文件夹,用于区分用户信息 dir = '/data/%s' % (self.emailInfo['email']) # 获取当前文件的绝对路径 abDir = os.path.abspath(os.path.join( os.path.dirname(__file__))).replace('\\', '/') dir = ("%s/%s") % (abDir, dir) if not os.path.exists(dir): os.mkdir(dir) print("文件夹创建成功:%s" % dir) self.close()
def itemClicked(self): my_currentItem = self.contactsList.currentItem() my_text = my_currentItem.text().split('\n') name = my_text[0] email = my_text[1] self.contacts = GetJsonInfo(self.contactsJsonName) qq = self.contacts[email].get('qq', '') remark = self.contacts[email].get('remark', '') weChat = self.contacts[email].get('weChat', '') phone = self.contacts[email].get('phone', '') self.contName.setText(name) self.contEmail.setText(email) self.contRemarkName.setText(remark) self.contPhone.setText(phone) self.contQQ.setText(qq) self.contWechat.setText(weChat) num = self.generateNum(email) self.contLogo.setStyleSheet( "border-image: url(:/avatar/Avatars/%d.jpg);" % num) print(self.contName.text())
def searchItemClicked(self): try: my_contacts = GetJsonInfo('contacts.json') my_currentItem = self.searchList.currentItem() my_text = my_currentItem.text().split('\n')[1].split('\n')[0] self.url = 'file:///' + os.path.abspath( os.path.join(os.path.dirname(__file__)) ) + r'/data/%s/%s.html' % (self.emailInfo['email'], my_text) self.url = self.url.replace('\\', '/') print(self.url) self.contName.setText(my_contacts[my_text]['name']) self.contEmail.setText(my_contacts[my_text]['fromAddr']) self.contEmailTime.setText(my_contacts[my_text]['date']) self.contEmailSubject.setText(my_text) self.mainForward.show() self.delEmail.show() self.mainReply.show() self.mainAttach.show() self.emailShow.setUrl(QtCore.QUrl(self.url)) self.attachList.hide() except Exception as e: print(str(e)) print("search")
def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog) # 导入qss样式表 # wait # 邮件数量 self.index = 0 self.page = 0 self.receiveWay = 0 # 判断是否已经的登录邮箱,若已经登录才能进行后续操作 self.login = 0 # 下面变量用于记录已经添加到列表中的值,从而避免重复添加 # 已接收邮件记录变量 self.isReceived = {} # 已删除邮件记录变量 self.isDeleted = {} # 草稿箱记录变量 self.isDraft = {} # 已发送邮件记录变量 self.isSent = {} # 指示当前邮件文件夹类型 self.currentFolder = 'receive' # 登录上次账号 try: start = time.time() self.emailInfo = GetJsonInfo('conf.json') # 获取当前文件的绝对路径 abDir = os.path.abspath(os.path.join( os.path.dirname(__file__))).replace('\\', '/') dir = "%s/data/%s" % (abDir, self.emailInfo['email']) if os.path.exists( dir) and self.emailInfo['email'] and self.emailInfo['pwd']: self.emailInfo["status"] = 1 SaveJsonInfo('conf.json', self.emailInfo) end = time.time() print('耗时:' + str(end - start)) self.mainUserName.setText(self.emailInfo['email']) self.mainlogin.setText('切换') self.login = 1 self.receiveJsonName = dir + "receive.json" self.sendJsonName = dir + "send.json" self.deleteJsonName = dir + "delete.json" self.draftJsonName = dir + "draft.json" num = self.generateNum(self.emailInfo['email']) self.headlogo.setStyleSheet(''' background-color: rgb(242, 242, 242); border-radius:40px; border-image: url(:/avatar/Avatars/%d.jpg); ''' % num) # t = threading.Thread(target=self.intervalRec(5)) # t.start() self.addQList(GetJsonInfo(self.receiveJsonName), 'emaillist') except Exception as e: print(e) self.emailInfo = { "pop3_server": "", "email": "", "pwd": "", "smtp_server": "", "status": 0 } SaveJsonInfo('conf.json', self.emailInfo) self.emailInfo = GetJsonInfo('conf.json') self.attachList.hide() self.mainForward.hide() self.delEmail.hide() self.mainReply.hide() self.mainAttach.hide() self.restoreEmail.hide() # 绑定emailList self.connect(self.emaillist, SIGNAL('itemClicked(QListWidgetItem *)'), self.emailItemClicked) # 绑定attachList self.connect(self.attachList, SIGNAL('itemClicked(QListWidgetItem *)'), self.attachItemClicked) # 绑定searchList self.connect(self.searchList, SIGNAL('itemClicked(QListWidgetItem *)'), self.searchItemClicked) # 绑定deleteList self.connect(self.deleteList, SIGNAL('itemClicked(QListWidgetItem *)'), self.deleteItemClicked) # 绑定draftList self.connect(self.draftList, SIGNAL('itemClicked(QListWidgetItem *)'), self.draftItemClicked) # 绑定sentList self.connect(self.sentList, SIGNAL('itemClicked(QListWidgetItem *)'), self.sentItemClicked)
def print_info(self, msg, indent=0): if indent == 0: global subject my_info = {} my_emailInfos = {} subject = msg.get('Subject', '') if subject: subject = self.decode_str(subject) my_info['subject'] = subject fromAddr = msg.get('From', '') if fromAddr: hdr, addr = parseaddr(fromAddr) name = self.decode_str(hdr) my_info["name"] = name my_info["fromAddr"] = addr date = msg.get('Date', '') if date: date = self.decode_str(date) date = parsedate(date) date = list(date) for i in range(len(date)): if len(str(date[i])) == 1: date[i] = '0' + str(date[i]) date = str(date[0]) + '.' + str(date[1]) + '.' + str( date[2]) + ' ' + str(date[3]) + ':' + str( date[4]) + ':' + str(date[5]) my_info['date'] = date if len(subject) > 30: my_emailInfos[subject[:30]] = my_info else: my_emailInfos[subject] = my_info my_oringinInfo = GetJsonInfo('contacts.json') my_oringinInfo.update(my_emailInfos) SaveJsonInfo('contacts.json', my_oringinInfo) # my_oringinInfo = GetJsonInfo('%s.json')%self.emailInfo['email'] # my_oringinInfo.update(my_emailInfos) # SaveJsonInfo('%s.json', my_oringinInfo)%self.emailInfo['email'] if (msg.is_multipart()): pass parts = msg.get_payload() for n, part in enumerate(parts): # print('%s--------------------' % (' ' * indent)) self.print_info(part, indent + 1) else: content_type = msg.get_content_type() # if content_type == 'text/plain' or content_type == 'text/html': if content_type == 'text/html': content = msg.get_payload(decode=True) charset = self.guess_charset(msg) # print(charset) if charset: print("解码咯,编码为:" + charset) try: content = content.decode('utf-8') except Exception as e: print(str(e)) content = content.decode(charset) print('解码成功') content = '<meta charset="utf-8">' + content + '<meta charset="utf-8">' # 保存为文件形式 if len(subject) > 30: emailname = subject[:30] else: emailname = subject # 用户存储目录,用于存储邮件html文件 dir = "data/%s" % self.emailInfo['email'] if not os.path.exists(dir): os.makedirs(dir) dir = dir + "/%s.html" % emailname with open(dir, 'wb') as f: f.write(content.encode('utf-8')) else: dir = dir + "/%s.html" % emailname with open(dir, 'wb') as f: f.write(content.encode('utf-8')) else: dir = "data/%s" % self.emailInfo['email'] if len(subject) > 30: emailname = subject[:30] else: emailname = subject # 若含有附件,则以邮件名创建附件文件夹 sonDir = dir + "/%s" % emailname print('有附件啦') filename = msg.get_filename() if filename: # h = email.Header.Header(filename) dh = decode_header(filename) fname = dh[0][0] charset = dh[0][1] print(type(fname)) print(fname) try: fname = fname.decode(charset) print(type(fname)) print(fname) except Exception as e: print(str(e)) fname = dh[0][0] print(type(fname)) print(fname) data = msg.get_payload(decode=True) if not os.path.exists(sonDir): os.makedirs(sonDir) sonDir = sonDir + "/%s" % fname with open(sonDir, 'wb') as f: f.write(data) else: sonDir = sonDir + "/%s" % fname print(sonDir) with open(sonDir, 'wb') as f: f.write(data) else: print("附件没名字??")