-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_texts.py~
25 lines (19 loc) · 1.05 KB
/
get_texts.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
import email, getpass, imaplib, os, re, string
detach_dir = '.' # directory where to save attachments (default: current)
user = '[email protected]'
pwd = 'fountain2088'
# connecting to the gmail imap server
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login(user,pwd)
m.select("[Gmail]/All Mail") # here you a can choose a mail box like INBOX instead
# use m.list() to get all the mailboxes
resp, items = m.search(None, '(SUBJECT "SMS")') # you could filter using the IMAP rules here (check http://www.example-code.com/csharp/imap-search-critera.asp)
items = items[0].split() # getting the mails id
for emailid in items:
resp, data = m.fetch(emailid, "(RFC822)") # fetching the mail, "`(RFC822)`" means "get the whole stuff", but you can ask for headers only, etc
email_body = data[0][2] # getting the mail content
mail = email.message_from_string(email_body) # parsing the mail content to get a mail object
t = re.search("\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}", mail["Subject"])
txt = t.group(0)
txtnum = re.sub("\D", "", txt)
print mail