Solved

Python email exception

Posted on 2009-05-05
4
326 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
Comment Utility
Add 'import email.errors' near where you imported 'email' and 'mailbox'.
0
 
LVL 8

Expert Comment

by:LunarNRG
Comment Utility
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
Comment Utility
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
Comment Utility
thanks LunarNRG!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

762 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

6 Experts available now in Live!

Get 1:1 Help Now