2010-07-05 152 views
11

如何从邮箱中删除邮件?我正在使用此代码,但不会删除这些字母。对不起我的英语不好。imap删除邮件

def getimap(self,server,port,login,password): 
    import imaplib, email 
    box = imaplib.IMAP4(server,port) 
    box.login(login,password) 
    box.select() 
    box.expunge() 
    typ, data = box.search(None, 'ALL') 
    for num in data[0].split() : 
     typ, data = box.fetch(num, '(UID BODY[TEXT])') 
     print num 
     print data[0][1] 
    box.close() 
    box.logout() 

回答

13

我想你应该标记的邮件被删除,第一。例如:

for num in data[0].split(): 
    box.store(num, '+FLAGS', '\\Deleted') 
box.expunge() 
2

如果您正在使用Gmail的过程是有点不同:

  1. 将其移至[Gmail] /垃圾箱文件夹。
  2. 从[Gmail] /垃圾桶文件夹(添加\删除标志)

中的[Gmail] /垃圾邮件和[Gmail] /垃圾桶所有邮件在30天后被删除删除它。 如果您从[Gmail] /垃圾邮件或[Gmail] /垃圾邮件中删除邮件,邮件将被永久删除。

还记得打电话EXPUNGE设置标签后删除。

10

这是删除所有的电子邮件收件箱中的工作代码:

import imaplib 
box = imaplib.IMAP4_SSL('imap.mail.microsoftonline.com', 993) 
box.login("[email protected]","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() 
5

下面的代码打印一些消息头字段,然后删除消息。

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 
1

这对我来说是什么工作,这是非常快,因为我不单独(店),删除每封电子邮件,但通过列表索引来代替。这适用于gmail个人以及企业(Google Apps for Business)。它首先选择要使用的文件夹/标签m.list()会显示所有可用的。然后,它搜索一年前的电子邮件,并执行垃圾邮件。然后它将所有垃圾邮件标记为删除标记并清除所有内容:

#!/bin/python 

import datetime 
import imaplib 

m = imaplib.IMAP4_SSL("imap.gmail.com") # server to connect to 
print "Connecting to mailbox..." 
m.login('[email protected]_gmail.com', 'your_password') 

print m.select('[Gmail]/All Mail') # required to perform search, m.list() for all lables, '[Gmail]/Sent Mail' 
before_date = (datetime.date.today() - datetime.timedelta(365)).strftime("%d-%b-%Y") # date string, 04-Jan-2013 
typ, data = m.search(None, '(BEFORE {0})'.format(before_date)) # search pointer for msgs before before_date 

if data != ['']: # if not empty list means messages exist 
    no_msgs = data[0].split()[-1] # last msg id in the list 
    print "To be removed:\t", no_msgs, "messages found with date before", before_date 
    m.store("1:{0}".format(no_msgs), '+X-GM-LABELS', '\\Trash') # move to trash 
    print "Deleted {0} messages. Closing connection & logging out.".format(no_msgs) 
else: 
    print "Nothing to remove." 

#This block empties trash, remove if you want to keep, Gmail auto purges trash after 30 days. 
print("Emptying Trash & Expunge...") 
m.select('[Gmail]/Trash') # select all trash 
m.store("1:*", '+FLAGS', '\\Deleted') #Flag all Trash as Deleted 
m.expunge() # not need if auto-expunge enabled 

print("Done. Closing connection & logging out.") 
m.close() 
m.logout() 
print "All Done."