| Line 12... |
Line 12... |
| 12 |
m = imaplib.IMAP4_SSL("imap.gmail.com")
|
12 |
m = imaplib.IMAP4_SSL("imap.gmail.com")
|
| 13 |
m.login(user,pwd)
|
13 |
m.login(user,pwd)
|
| 14 |
m.select("Inbox", True) # use m.list() to get all the mailboxes
|
14 |
m.select("Inbox", True) # use m.list() to get all the mailboxes
|
| 15 |
|
15 |
|
| 16 |
# you could filter using the IMAP rules here (check http://www.example-code.com/csharp/imap-search-critera.asp)
|
16 |
# you could filter using the IMAP rules here (check http://www.example-code.com/csharp/imap-search-critera.asp)
|
| 17 |
resp, items = m.search(None, 'SUBJECT', subject, 'SENTON', date)
|
17 |
resp, items = m.search(None, 'SUBJECT', subject, 'UNKEYWORD', 'processed')
|
| 18 |
items = items[0].split() # getting the mail ids
|
18 |
items = items[0].split() # getting the mail ids
|
| 19 |
items.sort(reverse=True)
|
- |
|
| 20 |
|
19 |
|
| 21 |
attachment_path = None
|
20 |
attachment_path = None
|
| 22 |
|
21 |
|
| 23 |
for emailid in items:
|
22 |
for emailid in reversed(items):
|
| 24 |
# fetching the mail, "`(RFC822)`" means "get the whole stuff", but you can ask for headers only, etc
|
23 |
# fetching the mail, "`(RFC822)`" means "get the whole stuff", but you can ask for headers only, etc
|
| 25 |
resp, data = m.fetch(emailid, "(RFC822)")
|
24 |
resp, data = m.fetch(emailid, "(RFC822)")
|
| 26 |
email_body = data[0][1] # getting the mail content
|
25 |
email_body = data[0][1] # getting the mail content
|
| 27 |
mail = email.message_from_string(email_body) # parsing the mail content to get a mail object
|
26 |
mail = email.message_from_string(email_body) # parsing the mail content to get a mail object
|
| 28 |
|
27 |
|
| 29 |
#Check if any attachments at all
|
28 |
#Check if any attachments at all
|
| 30 |
if mail.get_content_maintype() != 'multipart':
|
29 |
if mail.get_content_maintype() != 'multipart':
|
| 31 |
continue
|
30 |
continue
|
| 32 |
|
31 |
|
| 33 |
print "["+mail["From"]+"] :" + mail["Subject"]
|
32 |
print "["+mail["From"]+"] :" + mail["Subject"] + ":" + mail["Date"]
|
| 34 |
|
33 |
|
| 35 |
# we use walk to create a generator so we can iterate on the parts and forget about the recursive headach
|
34 |
# we use walk to create a generator so we can iterate on the parts and forget about the recursive headach
|
| 36 |
for part in mail.walk():
|
35 |
for part in mail.walk():
|
| 37 |
# multipart are just containers, so we skip them
|
36 |
# multipart are just containers, so we skip them
|
| 38 |
if part.get_content_maintype() == 'multipart':
|
37 |
if part.get_content_maintype() == 'multipart':
|
| Line 59... |
Line 58... |
| 59 |
fp.write(part.get_payload(decode=True))
|
58 |
fp.write(part.get_payload(decode=True))
|
| 60 |
fp.close()
|
59 |
fp.close()
|
| 61 |
|
60 |
|
| 62 |
#We are only expecting a single email per day with a given subject.
|
61 |
#We are only expecting a single email per day with a given subject.
|
| 63 |
break
|
62 |
break
|
| - |
|
63 |
try:
|
| - |
|
64 |
m.select("Inbox", False)
|
| - |
|
65 |
m.store(','.join(items), 'FLAGS', 'processed')
|
| - |
|
66 |
except:
|
| - |
|
67 |
pass
|
| 64 |
return attachment_path
|
68 |
return attachment_path
|
| 65 |
|
69 |
|