如何从邮箱中删除邮件?我正在使用此代码,但不删除字母.对不起我的英语不好.
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()
Run Code Online (Sandbox Code Playgroud)
cod*_*ark 19
这是删除收件箱中所有电子邮件的工作代码:
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()
Run Code Online (Sandbox Code Playgroud)
Jor*_*ril 16
我认为你应该首先标记要删除的电子邮件.例如:
for num in data[0].split():
box.store(num, '+FLAGS', '\\Deleted')
box.expunge()
Run Code Online (Sandbox Code Playgroud)
小智 6
以下代码打印一些邮件头字段,然后删除邮件.
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
Run Code Online (Sandbox Code Playgroud)
这对我有用,而且速度非常快,因为我不会单独删除每封电子邮件(存储),而是传递列表索引。这适用于 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('gmail@your_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."
Run Code Online (Sandbox Code Playgroud)