无法移动/删除电子邮件,但可以将其复制

时间:2018-12-26 14:03:28

标签: python imaplib

我可以将电子邮件复制到另一个文件夹,但是无法将其从INBOX(Office 365)删除

#!/usr/bin/python

import email, imaplib
user = 'user@example.com'
pwd = 'pass'

conn = imaplib.IMAP4_SSL("outlook.office365.com")
conn.login(user,pwd)
conn.select("Inbox")

resp, items = conn.uid("search",None, 'All')
items = items[0].split()
for emailid in items:
    resp, data = conn.uid("fetch",emailid, "(RFC822)")
    if resp == 'OK':
        email_body = data[0][1].decode('utf-8')
        mail = email.message_from_string(email_body)
        if mail.get_content_maintype() != 'multipart':
            continue
        if mail["Subject"].find("Linux") > 0:
            result = conn.uid('COPY', emailid, "Archive")
            print result
            if result[0] == 'OK':
             result = mov, data = conn.uid('STORE', emailid, '+FLAGS', 'Deleted Items')
             print result
             conn.expunge()

我在下面:

('OK', ['16 (FLAGS (\\Seen))'])

电子邮件保留在收件箱中

1 个答案:

答案 0 :(得分:0)

弄清楚了:必须在已删除邮件的前面添加\,起初我没有添加它,因为示例代码是针对Gmail的 因此,首先将电子邮件复制到所需的文件夹,然后将其从“收件箱”中删除:

if mail["Subject"].find("Linux") > 0:
            result = conn.uid('COPY', emailid, "Archive")
            print result
            if result[0] == 'OK':
             result = mov, data = conn.uid('STORE',emailid, '+FLAGS', '(\Deleted Items)')