簡體   English   中英

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 

我收到回復 [OK, [None]]

有人知道缺少什么嗎,我找到了很多解決方案,但它最適用於 Gmail。 我想盡可能處理所有郵件服務器。

您好,我已經進行了調試,可以看到我的收件箱是只讀權限。 我說得對嗎,這就是為什么不允許刪除電子郵件的原因?

    00:38.99 < b'* CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS BINARY MOVE SNIPPET=FUZZY PREVIEW=FUZZY PREVIEW STATUS=SIZE SAVEDATE LITERAL+ NOTIFY METADATA SPECIAL-USE QUOTA ACL RIGHTS=texk'
  00:38.99 < b'EFPF1 OK Logged in'
  00:38.99 > b'EFPF2 LIST "" *'
  00:39.04 < b'* LIST (\\HasNoChildren) "." Archive'
  00:39.05 < b'* LIST (\\HasNoChildren \\Trash) "." Trash'
  00:39.05 < b'* LIST (\\HasNoChildren \\Junk) "." Spam'
  00:39.05 < b'* LIST (\\HasNoChildren \\Sent) "." Sent'
  00:39.05 < b'* LIST (\\HasNoChildren \\Drafts) "." Drafts'
  00:39.05 < b'* LIST (\\HasNoChildren \\Junk) "." INBOX.Spam'
  00:39.05 < b'* LIST (\\HasChildren) "." INBOX'
  00:39.05 < b'EFPF2 OK List completed (0.003 + 0.000 + 0.002 secs).'
  00:39.05 > b'EFPF3 EXAMINE INBOX'
  00:39.08 < b'* FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)'
  00:39.08 < b'* OK [PERMANENTFLAGS ()] Read-only mailbox.'
  00:39.08 < b'* 33 EXISTS'
  00:39.08 < b'* 0 RECENT'
  00:39.08 < b'* OK [UIDVALIDITY 1640726753] UIDs valid'
  00:39.08 < b'* OK [UIDNEXT 437] Predicted next UID'
  00:39.08 < b'* OK [HIGHESTMODSEQ 448] Highest'
  00:39.08 < b'EFPF3 OK [READ-ONLY] Examine completed (0.001 + 0.000 secs).'
  00:39.08 > b'EFPF4 SEARCH ALL'
  00:39.12 < b'* SEARCH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33'
  00:39.12 < b'EFPF4 OK Search completed (0.001 + 0.000 secs).'
  00:39.12 > b'EFPF5 STORE 1 +FLAGS (\\Deleted)'
  00:39.16 < b'EFPF5 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.16 > b'EFPF6 STORE 2 +FLAGS (\\Deleted)'
  00:39.20 < b'EFPF6 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.20 > b'EFPF7 STORE 3 +FLAGS (\\Deleted)'
  00:39.24 < b'EFPF7 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.24 > b'EFPF8 STORE 4 +FLAGS (\\Deleted)'
  00:39.28 < b'EFPF8 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.28 > b'EFPF9 STORE 5 +FLAGS (\\Deleted)'
  00:39.31 < b'EFPF9 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.31 > b'EFPF10 STORE 6 +FLAGS (\\Deleted)'
  00:39.35 < b'EFPF10 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.35 > b'EFPF11 STORE 7 +FLAGS (\\Deleted)'
  00:39.39 < b'EFPF11 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.39 > b'EFPF12 STORE 8 +FLAGS (\\Deleted)'
  00:39.43 < b'EFPF12 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.43 > b'EFPF13 STORE 9 +FLAGS (\\Deleted)'
  00:39.47 < b'EFPF13 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.47 > b'EFPF14 STORE 10 +FLAGS (\\Deleted)'
  00:39.50 < b'EFPF14 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.50 > b'EFPF15 STORE 11 +FLAGS (\\Deleted)'
  00:39.54 < b'EFPF15 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.54 > b'EFPF16 STORE 12 +FLAGS (\\Deleted)'
  00:39.58 < b'EFPF16 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.58 > b'EFPF17 STORE 13 +FLAGS (\\Deleted)'
  00:39.62 < b'EFPF17 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.62 > b'EFPF18 STORE 14 +FLAGS (\\Deleted)'
  00:39.66 < b'EFPF18 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.66 > b'EFPF19 STORE 15 +FLAGS (\\Deleted)'
  00:39.69 < b'EFPF19 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.69 > b'EFPF20 STORE 16 +FLAGS (\\Deleted)'
  00:39.73 < b'EFPF20 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.73 > b'EFPF21 STORE 17 +FLAGS (\\Deleted)'
  00:39.77 < b'EFPF21 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.77 > b'EFPF22 STORE 18 +FLAGS (\\Deleted)'
  00:39.81 < b'EFPF22 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.81 > b'EFPF23 STORE 19 +FLAGS (\\Deleted)'
  00:39.85 < b'EFPF23 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.85 > b'EFPF24 STORE 20 +FLAGS (\\Deleted)'
  00:39.88 < b'EFPF24 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.88 > b'EFPF25 STORE 21 +FLAGS (\\Deleted)'
  00:39.92 < b'EFPF25 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.92 > b'EFPF26 STORE 22 +FLAGS (\\Deleted)'
  00:39.96 < b'EFPF26 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:39.96 > b'EFPF27 STORE 23 +FLAGS (\\Deleted)'
  00:40.00 < b'EFPF27 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:40.00 > b'EFPF28 STORE 24 +FLAGS (\\Deleted)'
  00:40.04 < b'EFPF28 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:40.04 > b'EFPF29 STORE 25 +FLAGS (\\Deleted)'
  00:40.08 < b'EFPF29 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:40.08 > b'EFPF30 STORE 26 +FLAGS (\\Deleted)'
  00:40.11 < b'EFPF30 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:40.11 > b'EFPF31 STORE 27 +FLAGS (\\Deleted)'
  00:40.15 < b'EFPF31 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:40.15 > b'EFPF32 STORE 28 +FLAGS (\\Deleted)'
  00:40.19 < b'EFPF32 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:40.19 > b'EFPF33 STORE 29 +FLAGS (\\Deleted)'
  00:40.23 < b'EFPF33 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:40.23 > b'EFPF34 STORE 30 +FLAGS (\\Deleted)'
  00:40.27 < b'EFPF34 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:40.27 > b'EFPF35 STORE 31 +FLAGS (\\Deleted)'
  00:40.30 < b'EFPF35 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:40.30 > b'EFPF36 STORE 32 +FLAGS (\\Deleted)'
  00:40.34 < b'EFPF36 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:40.34 > b'EFPF37 STORE 33 +FLAGS (\\Deleted)'
  00:40.38 < b'EFPF37 OK Store ignored with read-only mailbox (0.001 + 0.000 secs).'
  00:40.38 > b'EFPF38 EXPUNGE'
  00:40.42 < b'EFPF38 OK Expunge completed (0.001 + 0.000 secs).'
  00:40.42 > b'EFPF39 CLOSE'
  00:40.46 < b'EFPF39 OK Close completed (0.001 + 0.000 secs).'
  00:40.46 > b'EFPF40 LOGOUT'
  00:40.49 < b'* BYE Logging out'
  00:40.49 BYE response: b'Logging out'
All emails are deleted and finish

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM