On Thu, 20 Dec 2007 09:31:10 +0000, Robert Latest wrote: > 1. Why can I get the 'subject' and 'from' header field unsig the [] > notation, but not 'to'? When I print Message.keys I get a list of all > header fields of the message, including 'to'. What's the difference > between message['to'] and message.get('to')?
message['to'] looks up the key 'to', raising an exception if it doesn't exist. message.get('to') looks up the key and returns a default value if it doesn't exist. See help(message.get) for more detail. > 2. Why can't I call the get_payload() method on the message? What I get > is this cryptic error: "AttributeError: Message instance has no > attribute 'get_payload'". I'm trying to call a method here, not an > attribute. It makes no difference if I put parentheses after get_payload > or not. I looked into the email/Message module and found get_payload > defined there. All methods are attributes (although the opposite is not the case), so if a method doesn't exist, you will get an AttributeError. The email.Message.Message class has a get_payload, but you're not using that class. You're using mailbox.UnixMailbox, which returns an instance of rfc822.Message which *doesn't* have a get_payload method. Damned if I can work out how to actually *use* the email module to read an mbox mail box. I might have to RTFM :( http://docs.python.org/lib/module-email.html http://docs.python.org/lib/module-mailbox.html *later* Ah! The Fine Manual is some help after all. Try this: # copied from http://docs.python.org/lib/mailbox-deprecated.html import email import email.Errors import mailbox def msgfactory(fp): try: return email.message_from_file(fp) except email.Errors.MessageParseError: # Don't return None since that will # stop the mailbox iterator return '' fp = file('mymailbox', 'rb') mbox = mailbox.UnixMailbox(fp, msgfactory) for message in mbox: print message.get_payload() But note that message.get_payload() will return either a string (for single part emails) or a list of Messages (for multi-part messages). -- Steven -- http://mail.python.org/mailman/listinfo/python-list