[英]Use IMAP4 in python to move mail from a mailbox to another mailbox
我是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-messgt,但是我不知道該怎么做。
我已經嘗試了這一天的大部分時間,
""" 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%...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.