imap - how to delete messages

后端 未结 9 647
孤独总比滥情好
孤独总比滥情好 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: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 
    

提交回复
热议问题