I am moving from C# to Python, and I am guessing I am stepping on some namesapce issue, but I can't find the problem. Here is the current class that is giving me error (on line 41)
import imaplib
import os
import email
class EmailWrapper:
hostname = None # herp
username = None # derp
password = None # might encrypt this if there is time
def __init__(self, host, user, passwd):
self.hostname = host
self.username = user
self.password = passwd
# Create connection and return it
def connect(self, verbose=True):
if verbose: print 'Connecting to ', self.hostname
connection = imaplib.IMAP4_SSL(self.hostname)
if verbose: print 'Logging in as ', self.username
connection.login(self.username, self.password)
if verbose: print 'Selecting Inbox.'
connection.select('Inbox')
return connection
# Grab last email in inbox and return it from connection
def get_last_email(self, c):
# Get list of emails
result, data = c.search(None, "ALL")
# get last ID
ids = data[0]
idList = ids.split()
lastEmailID = idList[-1]
# Fetch email
result, data = c.fetch(lastEmailID, "(RFC822)")
# Seclect body and return it
rawEmail = data[0][1]
emailMessage = email.message_from_string(rawEmail)
return emailMessage
def close_connection(self, c):
c.close()
c.logout()
Any time I call get_last_email I get the error "AttributeError: 'module' object has no attribute 'message_from_string'". Any ideas would be appreciated.
Thanks
dir(email)? You don't happen to have a file calledemail.pyin the same folder, right?