5

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

5
  • 1
    Does the example here work when you put it in a interactive prompt? Commented Apr 21, 2016 at 1:55
  • Yes, no error there. Commented Apr 21, 2016 at 1:58
  • 3
    What's the output of dir(email)? You don't happen to have a file called email.py in the same folder, right? Commented Apr 21, 2016 at 2:02
  • 2
    Son of a **** mother ****** I did! It was the first script I was messing with. Jesus, thanks. I R the dumb. Thank you @jDo - that was the problem. Commented Apr 21, 2016 at 2:08
  • 1
    @OmegaNine I knew it! :D You're welcome! Glad it's solved Commented Apr 21, 2016 at 2:10

1 Answer 1

7

The problem, as pointed out by @jDo, was that i still had an empty email.py file sitting around in the project directory. Thanks!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.