Imaplib 不删除电子邮件

Imaplib not deleting emails

我需要从邮件服务器中删除所有电子邮件,我已经编写了一些代码来处理这个问题。但是它不起作用我已经尝试了所有方法仍然无法从邮件服务器中删除它。

所以我需要问问是否有人知道我缺少什么代码,所以它会删除 Plesk/kolab 邮件系统上的电子邮件。

我的密码是


from lib.Enums.status import Status


class Imap:
    mailserver = None
    port = 993

    _conn = ''

    def __init__(self, mailserver=None, port=993):
        self.mailserver = mailserver
        self.port = port


    def connect(self):
        if self.mailserver is not None:
            try:
                self._conn = imaplib.IMAP4_SSL(self.mailserver, self.port)
                return self
            except imaplib.IMAP4 as e:
                # TODO: make log
                return e.error

        return None

    # Login
    def login(self, username=None, password=None):
        if username is not None and password is not None:
            try:
                self._conn.login(username, password)
                return self
            except imaplib.IMAP4_SSL.error as e:
                # TODO: make log
                return Status.FAIL


    def get_all_message_new(self):
        return self._conn.search(None, "ALL")

    def delete_message(self, mail):
        # mark the mail as deleted
        return self._conn.store(mail, '+FLAGS', '\Deleted')

    # Delete permanently message
    def delete_permanently(self):
        self._conn.expunge()


电子邮件处理程序:

def delete_emails(self, info_obj):

        # Mailserver transfer from
        mail_server = Imap(info_obj.url).connect()
        mail_server.login(info_obj.username, info_obj.password)

        # Get folder list from mail server
        directories = mail_server.get_directories()

        status, data = mail_server.select('INBOX')

        typ, msg_ids = mail_server.get_all_message_new()

        for mail in msg_ids[0].decode().split():
            # mark the mail as deleted
            mov, data = mail_server.delete_message(mail)
            print(data)

        mail_server.delete_permanently()
        mail_server.close()
        mail_server.logout()

        return Status.SUCCESS 

我收到回复 [好的,[None]]

有没有人知道缺少什么,我找到了很多解决方案,但它最适用于 Gmail。我想尽可能处理所有邮件服务器。

此问题的解决方案是为只读设置 false 而不是 true

   # Get all raw messages from current directory
def select(self, dir=None,readonly=False):
    if dir is not None:
        return self._conn.select(dir, readonly)