0

I can copy email to another folder but unable to delete it from 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()

I'm getting below:

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

and emails stay in Inbox

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

Figured it out: had to add \ in front of Deleted Items, i didn't add it at first because sample code was for Gmail So first copy emails to desired folder and then delete it from Inbox:

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)')
  • The flag you want is “\Deleted”, it is not a folder name. You are adding two flags: \Deleted, and Items. – Max Dec 26 '18 at 17:02