Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
64 lines (49 sloc) 2.03 KB
import imaplib
import email
import ssl
import socket
#import time
#start = time.time()
def removeVowels(str):
vowels = ('a', 'e', 'i', 'o', 'u')
return ''.join([l for l in str if l not in vowels]);
def getmail(email_address, password):
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(email_address, password)
mail.list()
mail.select('inbox')
typ, data = mail.search(None, 'ALL') #inefficient, but only option
ids = data[0]
id_list = ids.split()
#TODO: change this based on user input to e.g. -10 to get messages 10-15
latest_email_id = int(id_list[-1])
result = ""
#iterate through messages in decending order starting with latest_email_id
#the '-1' dictates reverse looping order
for i in range( latest_email_id, latest_email_id-5, -1 ): #changed to return 5 emails
typ, data = mail.fetch(i, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
varSubject = msg['subject']
varFrom = msg['from']
#remove the brackets around the sender email address, remove vowels too?
varFrom = varFrom.replace('<', '')
varFrom = varFrom.replace('>', '')
#Text messages allow 160 Characters
if len( varSubject ) > 15:
varSubject = varSubject[0:15]
if len(varFrom) > 14:
varFrom = varFrom[0:14]
#result += '[' + removeVowels(varFrom.split()[-1]) + ']' + ' ' + removeVowels(varSubject) +"\n" #saves characters
result += "[" + varFrom.split()[-1] + "]" + ' ' + varSubject +"\n"
#end = time.time()
#print end - start
# GAE timeout at 30 sec
# twilio will only wait for 15
#changed to 10 emails, time = 9.27 seconds, still close. . .
#characters = 612 for 10 emails.
#10 emails, 32 chars each takes 6.7 seconds
# removing vowels for 10 emails results in ~200 characters
mail.logout()
return result