-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmailbox.py
More file actions
71 lines (54 loc) · 1.99 KB
/
Copy pathgmailbox.py
File metadata and controls
71 lines (54 loc) · 1.99 KB
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
import email
import getpass
import imaplib
import datetime
def gmail_imap_login(email, password=None):
""" Log in to the gmail imap server. Returns None if something went wrong in
imaplib otherwise it will return the mailbox object or error out if the
error isn't in imaplib.
If you're uncomfortable saving your password in a file, you can leave
that parameter empty and you'll be asked for your password via the
command line (thank you getpass)
"""
try:
M = imaplib.IMAP4_SSL('imap.gmail.com')
except imaplib.IMAP4.error:
print "unable to connect to imap server"
return
try:
M.login(email, password or getpass.getpass())
print "logged in"
return M
except imaplib.IMAP4.error:
print "Something went wrong"
return
def process_mailbox(M):
messages = []
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
continue
msg = email.message_from_string(data[0][1])
date_tuple = email.utils.parsedate_tz(msg['Date'])
if date_tuple:
local_date = datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple))
msg_date = local_date.strftime("%a, %d %b %Y %H:%M:%S")
else:
msg_date = msg['Date']
message = dict(
subject=msg['Subject'],
sender_name=' '.join(msg['From'].split()[:-1]).strip('"'),
email=msg['From'].split()[-1].strip("<>"),
date=msg_date
)
# print 'Message %s: %s' % (num, msg['Subject'])
# print 'From', msg['From'].split()[-1].strip("<>")
# print 'Raw Date:', msg['Date']
# print message
messages.append(message)
return messages