import poplib
import email
def get_emails_for_account(email_address):
messages_wot_we_want = {}
popconnection.user(user)
popconnection.pass_('password')
# Find out how many messages are waiting
numMessages = len(popconnection.list()[1])
# Iterate through the messages
for i in range(numMessages):
# retr will be called three times for each email message in the j loop below.
#
# The first call will return a string telling you how many bytes (i.e., octets)
# are in the message. The string will look like this: OK <byte count> octets
# (where <byte count> is the total number of bytes in the message).
#
# The second call will return a list containing the elements of the message.
#
# The third call will return an integer containing the total number of
# bytes in the message
for j in popconnection.retr(i+1):
# We are interested only in the contents of the list. And we need to put the
# list contents into a string, because that is what message_from_string expects.
# We must also be sure to put a line break at the end of the substring that
# represents each list element, as message_from_string relies on line breaks
# to parse the email message. The k loop below builds the string from the list.
if type(j) == list:
numElements = len(j)
outString = ""
for k in range(numElements):
outString += j[k]
outString += '\n'
#if email.
message = email.message_from_string(outString)
# Now that we have got the contents of the email into an email object, we can
# access the logical elements of the email at will, in any order. The call to
# get_payload() is to retrieve the body of the message.
#print message['Subject']
#print message['From']
if message['To'] == email_address:
messages_wot_we_want[message['Date']] = message
#print message.get_payload()
# Strictly speaking, all we need to do on quitting is to disconnect, but in some
# applications it would be a good idea to delete the email from the server.
popconnection.quit()
return messages_wot_we_want
def get_email_information(email_address, type_of_email):
""" method to look up emails for the named account, and using the type of email, return
the information in the email."""
emails = get_emails_for_account(email_address)
# this is a dictionary of emails, with the email datetime as the key.
email_types = { "welcome" : "You are so in the know",
"password_reset" : "Shhh! Your password",
"order" : "order",
"order_cancellation" : "cancel",
"returns" : "return",
}
# now sort and filter the old messages
message_order = emails.keys()
message_order.sort()
message_order.reverse()
# we now have the messages in reverse order so we process the latest one first.
for key in message_order:
message = emails[key]
if email_types[type_of_email] == message['subject']:
if message['subject'] == "You are so in the know":
# welcome email, ignore it.
return "confirmed"
if message['subject'] == "Shhh! Your password":
# password reset
email_body = message.get_payload()
# chop the password out of the message body
password = email_body.split('We have a temporary password for you as requested: ')[1].splitlines()[0].strip()
return password