-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.py
34 lines (28 loc) · 920 Bytes
/
fetch.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
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Brianna Birman on 2012-06-19.
"""
import imaplib
import sys
import os
import pickle
def main():
gmail = imaplib.IMAP4_SSL('imap.gmail.com')
gmail.login('[email protected]', 'testpassword')
gmail.debug=4
gmail.select('[Gmail]/Chats', readonly=True) # connect to chat section, must be readonly
result, uids = gmail.uid('search', None, "ALL") #all messages within chats
#result is ok, no, bad, etc.
id_list = uids[0].split() # ids is a space separated string
raw_emails = []
FILE = open('data.txt', 'wb')
for id in id_list: #goes from oldest to newest
result, data = gmail.uid('fetch', id, '(RFC822)') # fetch the email body (RFC822) for the given ID
raw_email = data[0][1] #raw text of the whole email
raw_emails.append(raw_email)
pickle.dump(raw_emails, FILE)
FILE.close()
gmail.logout()
if __name__ == '__main__':
main()