-
Notifications
You must be signed in to change notification settings - Fork 1
/
search-mail.py
77 lines (60 loc) · 2.25 KB
/
search-mail.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import sys
import argparse
import getpass
import imaplib
import json
import os.path
def main(argv):
defaults = {}
CONFIG_PATH = os.path.join(os.environ['HOME'], '.mailpy')
if os.path.isfile(CONFIG_PATH):
with open(CONFIG_PATH) as f:
defaults = json.load(f)
def add_arg(parser, name, default_default=None):
d = defaults.get(name, default_default)
if d is not None:
parser.add_argument('--' + name, default=d)
else:
parser.add_argument('--' + name, required=True)
parser = argparse.ArgumentParser()
add_arg(parser, 'username')
add_arg(parser, 'password', '-')
add_arg(parser, 'host')
add_arg(parser, 'port', 993)
parser.add_argument('--delete-messages', default=False, action='store_true')
parser.add_argument('--print-headers', default=False, action='store_true')
parser.add_argument('--to', default=None)
parser.add_argument('--from', dest='from_addr', default=None)
parser.add_argument('--subject', default=None)
args = parser.parse_args()
search_criteria = []
if args.to != None:
search_criteria.extend(['TO', args.to])
if args.from_addr != None:
search_criteria.extend(['FROM', args.from_addr])
if args.subject != None:
search_criteria.extend(['SUBJECT', args.subject])
if len(search_criteria) == 0:
print >>sys.stderr, "No search criteria specified"
sys.exit(1)
if args.password == '-':
args.password = getpass.getpass()
imap = imaplib.IMAP4_SSL(args.host, args.port)
imap.login(args.username, args.password)
imap.select('INBOX')
_, uids = imap.uid('SEARCH', *search_criteria)
uids = uids[0].split()
print >>sys.stderr, 'Matches %d messages' % len(uids)
if not args.delete_messages and not args.print_headers:
return
for i, uid in enumerate(uids):
if i % 10 == 0:
print (i + 1), 'of', len(uids)
if args.print_headers:
print imap.uid('FETCH', uid, '(BODY.PEEK[HEADER])')[1][0][1].strip()
if args.delete_messages:
imap.uid('STORE', uid, '+FLAGS', '(\\Deleted)')
if args.delete_messages and uids:
imap.expunge()
if __name__ == '__main__':
main(sys.argv)