在 python 中使用 IMAP4 将邮件从一个邮箱移动到另一个邮箱
Use IMAP4 in python to move mail from a mailbox to another mailbox
我是 Python 的新手,但我需要构建一个 IMAP 处理程序,我现在已经构建了,所以我可以发送邮件,我现在可以保存附件。
下一步我需要在阅读邮件并将附件下载到我的计算机后移动邮件,我已经构建了一个 class 来处理它,现在你可以在我的代码中看到发生了什么这里。
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,但我不知道该怎么做。
今天我已经尝试了大部分时间,解决方案是
""" 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%....
我是 Python 的新手,但我需要构建一个 IMAP 处理程序,我现在已经构建了,所以我可以发送邮件,我现在可以保存附件。
下一步我需要在阅读邮件并将附件下载到我的计算机后移动邮件,我已经构建了一个 class 来处理它,现在你可以在我的代码中看到发生了什么这里。
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,但我不知道该怎么做。
今天我已经尝试了大部分时间,解决方案是
""" 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%....