imap - how to delete messages

后端 未结 9 645
孤独总比滥情好
孤独总比滥情好 2020-12-14 08:00

How can I delete messages from the mail box? I am using this code, but the letters are not removed. Sorry for my English.

def getimap(self,server,port,login,         


        
相关标签:
9条回答
  • 2020-12-14 08:44

    If you are using GMail the process is a bit different:

    1. Move it to the [Gmail]/Trash folder.
    2. Delete it from the [Gmail]/Trash folder (Add \Delete flag)

    All emails in [Gmail]/Spam and [Gmail]/Trash are deleted after 30 days. If you delete a message from [Gmail]/Spam or [Gmail]/Trash, it will be deleted permanently.

    Remember also to call EXPUNGE after setting the tag Deleted.

    0 讨论(0)
  • 2020-12-14 08:51

    This is the working code for deleting all emails in your inbox:

    import imaplib
    box = imaplib.IMAP4_SSL('imap.mail.microsoftonline.com', 993)
    box.login("user@domain.com","paswword")
    box.select('Inbox')
    typ, data = box.search(None, 'ALL')
    for num in data[0].split():
       box.store(num, '+FLAGS', '\\Deleted')
    box.expunge()
    box.close()
    box.logout()
    
    0 讨论(0)
  • 2020-12-14 08:51

    The following code prints some message header fields and then delete message.

    import imaplib
    from email.parser import HeaderParser
    m = imaplib.IMAP4_SSL("your_imap_server")
    m.login("your_username","your_password")
    # get list of mailboxes
    list = m.list()
    # select which mail box to process
    m.select("Inbox") 
    resp, data = m.uid('search',None, "ALL") # search and return Uids
    uids = data[0].split()    
    mailparser = HeaderParser()
    for uid in uids:
        resp,data = m.uid('fetch',uid,"(BODY[HEADER])")        
        msg = mailparser.parsestr(data[0][1])       
        print (msg['From'],msg['Date'],msg['Subject'])        
        print m.uid('STORE',uid, '+FLAGS', '(\\Deleted)')
    print m.expunge()
    m.close() # close the mailbox
    m.logout() # logout 
    
    0 讨论(0)
提交回复
热议问题