我是Python的新手,但我需要构建一个IMAP处理程序,我现在已经构建,所以我可以把邮件拿出来,我现在可以保存附件。
下一步我需要移动邮件并将其附加到我的计算机后,我已经构建了一个类来处理它以及现在您可以在我的代码中看到的情况。
class IMAPHandler
mailFolder = "INBOX"
mailFolderCopyTo = "INOBX/Parsed"
localPath = "./tmp"
""" Defined server, username and password """
def __init__(self, server, username, password): ...
""" Connect to your IMAP mailbox """
def __login(self): ...
""" Close connection to your IMAP mailbox """
def __close(self): ...
""" Get all mailbox messegt """
def getAll(self):
self.__login()
rv, data = self.mailbox.search(None, 'UNSEEN', '(HEADER FROM "{mail-from}")')
if rv != 'OK':
print("No messages found!")
for num in data[0].split():
rv, data = self.mailbox.fetch(num, '(RFC822)')
print(rv)
email_body = data[0][1]
mail = email.message_from_string(email_body)
print "["+mail["From"]+"] :" + mail["Subject"]
print "-"
self.__saveAttachedFiles(mail)
self.__close()
def __saveAttachedFiles(self,mail): ...
self.__saveAttachedFiles(mail)
之后我需要移动mail-messegt,但我不知道我是怎么做的。
答案 0 :(得分:0)
我已经尝试了这一天的大部分时间,已解决的是
""" Move mail messegt to parsed folder after its handle """
def __moveMailToParsedFolder(self,num):
mail_uid = num
apply_lbl_msg = self.mailbox.copy(mail_uid, self.mailFolderCopyTo)
if apply_lbl_msg[0] == 'OK':
self.mailbox.store(mail_uid, '+FLAGS', '\Deleted')
self.mailbox.expunge()
现在我只需知道为什么每次只花50%......