-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathimaplib_delete_messages.py
46 lines (37 loc) · 1.16 KB
/
imaplib_delete_messages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
# encoding: utf-8
#
# Copyright (c) 2008 Doug Hellmann All rights reserved.
#
"""
"""
#end_pymotw_header
import imaplib
import imaplib_connect
from imaplib_list_parse import parse_list_response
with imaplib_connect.open_connection() as c:
c.select('Example.Today')
# What ids are in the mailbox?
typ, [msg_ids] = c.search(None, 'ALL')
print('Starting messages:', msg_ids)
# Find the message(s)
typ, [msg_ids] = c.search(
None,
'(SUBJECT "subject goes here")',
)
msg_ids = ','.join(msg_ids.decode('utf-8').split(' '))
print('Matching messages:', msg_ids)
# What are the current flags?
typ, response = c.fetch(msg_ids, '(FLAGS)')
print('Flags before:', response)
# Change the Deleted flag
typ, response = c.store(msg_ids, '+FLAGS', r'(\Deleted)')
# What are the flags now?
typ, response = c.fetch(msg_ids, '(FLAGS)')
print('Flags after:', response)
# Really delete the message.
typ, response = c.expunge()
print('Expunged:', response)
# What ids are left in the mailbox?
typ, [msg_ids] = c.search(None, 'ALL')
print('Remaining messages:', msg_ids)