Solved

Python email exception

Posted on 2009-05-05
4
331 Views
Last Modified: 2012-05-06
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
Comment
Question by:davidw88
  • 3
4 Comments
 
LVL 8

Expert Comment

by:LunarNRG
ID: 24309592
Add 'import email.errors' near where you imported 'email' and 'mailbox'.
0
 
LVL 8

Expert Comment

by:LunarNRG
ID: 24309604
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
 
LVL 8

Accepted Solution

by:
LunarNRG earned 125 total points
ID: 24309644
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
 

Author Comment

by:davidw88
ID: 24309847
thanks LunarNRG!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question