• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

Python email exception

Hi experts,

I try to use the attached code to retrieve e-mails, however I got

david@xadapp3:~/test$ sudo python EmailContent.py
Traceback (most recent call last):
  File "EmailContent.py", line 52, in <module>
    email.mailstats('geek')
  File "EmailContent.py", line 26, in mailstats
    str  = self.msgfactory(fp)
  File "EmailContent.py", line 14, in msgfactory
    except email.errors.MessageParseError:
AttributeError: EmailContent instance has no attribute 'errors'


I have imported "email", "mailbox", etc.

Anybody knows why I got an error?

Thanks so much.

def msgfactory(self, fp):
        try:
            return email.message_from_file(fp)
        except email.errors.MessageParseError:
        # Don't return None since that will
        # stop the mailbox iterator
            return ''
 
    def mailstats(self, user):
        if os.path.isfile('/var/spool/mail/'+user):
            fp=open('/var/spool/mail/'+user)
        else:
            print 'I cannot open mail for user',user
            sys.exit(1)
 
        str  = self.msgfactory(fp)
        mbox = mailbox.UnixMailbox(fp, str)
 
      ..................

Open in new window

0
davidw88
Asked:
davidw88
  • 3
1 Solution
 
LunarNRGCommented:
Add 'import email.errors' near where you imported 'email' and 'mailbox'.
0
 
LunarNRGCommented:
Have you defined a variable named 'email' anywhere in your code? Something like this ...

import email
import email.errors
import mailbox


email = email.Message(...)

...
0
 
LunarNRGCommented:
If so, you should avoid rebinding names of imported modules. Otherwise, how can you refer to the module named 'email', after you assign an object to the name 'email' (ie. email = 'something else but not the module').
0
 
davidw88Author Commented:
thanks LunarNRG!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now