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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#!/usr/bin/python
import imaplib
import email
import datetime
def process_mailbox(M):
rv, data = M.search(None, "UNSEEN")
if rv != 'OK':
print "No messages found!"
return
for num in data[0].split():
# rv, data = M.fetch(num, '(RFC822)')
rv, data = M.fetch(num, '(BODY.PEEK[])')
if rv != 'OK':
print "ERROR getting message", num
return
msg = email.message_from_string(data[0][1])
print 'From:', msg['From']
print 'Subject: %s' % (msg['Subject'])
print 'Raw Date:', msg['Date']
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_SSL("imap.cern.ch", 993)
M.login("pavel.makhov@cern.ch","cB#h8g!2n")
# status, counts = M.status("INBOX","(MESSAGES UNSEEN)")
# # status, counts = M.status("INBOX/!Edh-team","(MESSAGES UNSEEN)")
# unread = counts[0].split()[4][:-1]
# # print(status)
# print(unread)
rv, data = M.select("INBOX")
if rv == 'OK':
print "Processing mailbox...\n"
process_mailbox(M) # ... do something with emails, see below ...
M.close()
M.logout()
# M.select("INBOX")
# status, response = M.search('INBOX', '(UNSEEN)')
# unread_msg_nums = response[0].split()
# # Print the count of all unread messages
# print len(unread_msg_nums)
# print 'HEADER:'
# typ, msg_data = M.fetch('1', '(BODY.PEEK[HEADER])')
# for response_part in msg_data:
# if isinstance(response_part, tuple):
# print response_part[1]
# da = []
# # for e_id in unread_msg_nums:
# _, response = M.fetch(1, '(BODY.PEEK[TEXT])')
# # _, response = M.fetch(e_id, '(UID BODY[TEXT])')
# da.append(response[0][1])
# print da
# typ, data = M.select ("INBOX/!Edh-team")
# status, response = imap.search('INBOX', '(UNSEEN)')
# unread_msg_num = response[0].split()
# Print the count of all unread messages
#print typ
#print data
# for num in data[0].split():
# typ, data = M.fetch(num, '(RFC822)')
# print 'Message %s\n%s\n' % (num, data[0][1])
# for response_part in data:
# if isinstance(response_part, tuple):
# msg = email.message_from_string(response_part[1])
# for header in [ 'subject', 'to', 'from' ]:
# print '%-8s: %s' % (header.upper(), msg[header])
# M.close()
# M.logout()
|