-
Notifications
You must be signed in to change notification settings - Fork 0
/
imaptest.py
67 lines (53 loc) · 1.63 KB
/
imaptest.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
import imaplib
import sys
import email
import email.header
import datetime
email_account = 'username'
email_password = 'p@ssw0rd'
email_folder = 'INBOX/toHeliumV'
email_server = 'demo.siedl.net'
def process_mailbox(M):
"""
Do something with emails messages in the folder.
For the sake of this example, print some headers.
"""
rv, data = M.search(None, "ALL")
if rv != 'OK':
print("No messages found!")
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print("ERROR getting message", num)
return
msg = email.message_from_bytes(data[0][1])
decode = email.header.decode_header(msg['Subject'])[0]
subject = decode[0]
print('Message %s: %s' % (num, subject))
print('Raw Date:', msg['Date'])
# Now convert to local date-time
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("%a, %d %b %Y %H:%M:%S"))
M = imaplib.IMAP4(email_server)
try:
rv, data = M.login(email_account, email_password)
except imaplib.IMAP4.error as e:
print('Login failed: {}'.format(e))
sys.exit(1)
print(rv, data)
rv, mailboxes = M.list()
if rv == 'OK':
print('Mailboxes:')
print(mailboxes)
rv, data = M.select(email_folder)
if rv == 'OK':
print('Processing mailbox {} ..'.format(email_folder))
process_mailbox(M)
M.close()
else:
print('Unable to open mailbox: {}'.format(rv))
M.logout()