Solved

Python email exception

Posted on 2009-05-05
4
328 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now