我读过一些解决方案,但没有一个有效,可能是因为Gmail,我不确定,问题是我想把我的电子邮件从收件箱移到垃圾箱,我就是这样做的:def process_mailbox():
message={}
M = imaplib.IMAP4_SSL('imap.gmail.com')
try:
M.login('myemail@gmail.com', 'mypassword')
except imaplib.IMAP4.error:
print "LOGIN FAILED!!! "
# ... exit or deal with failure...
rv, mailboxes = M.list()
print mailboxes
if rv == 'OK':
M.select("INBOX")
rv, data = M.search(None, "ALL")
if rv != 'OK':
print "No messages found!"
for num in data[0].split(): #Read all the mails
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print "ERROR getting message", num
return
msg = email.message_from_string(data[0][1])
#print 'Subject %s: %s' % (num, msg['Subject'])
message['Subject']=msg['Subject']
print 'Subject: '+message['Subject']
if msg.get_content_type() == "text_plain": #No Multipart messages
body = msg.get_payload()
message['Body']=body
else: #Multipart messages
for part in msg.walk():
if part.get_content_type() == "text/plain": # ignore attachments/html
message['Body']=body
#print message['Body']
date_tuple = email.utils.parsedate_tz(msg['Date'])
if date_tuple:
local_date = datetime.datetime.fromtimestamp(
email.utils.mktime_tz(date_tuple))
print "Local Date:", local_date.strftime("%Y-%m-%d")
message['Date']=local_date.strftime("%Y-%m-%d")
#send_mail(message)
#insert_vulnerability_mail(message['Subject'],message['Date'],message['Body'].encode('utf-8'))
# M.store(num, '+FLAGS', '\\Deleted')
M.copy(num,'[Gmail]/Trash')
M.close()
M.logout()
所以,如你所见,是:M.copy(num,[Gmail]/Trash'),结果是我移动了一些电子邮件,比如说,如果我有7个,我会从7中移动4个,那么我会得到以下错误:
^{pr2}$
我不明白,因为当我下一次执行这个程序时,我会移动更多的电子邮件,在另一封邮件上得到错误,执行后我会移动所有的东西,但我必须执行好几次。在
有人知道发生了什么吗?提前谢谢