Email Parse and Python Cookbook

Matthew Dixon Cowles matt at mondoinfo.com
Thu Nov 7 22:24:24 EST 2002


On Thu, 7 Nov 2002 14:54:29 -0800, David McInnis
<david at dataovation.com> wrote:

> My problem is this.  I am using a sample out of the Python Cookbook
> but this example fails on some messages.  Works on most.

> mydomain.com/support/Maildir/cur/1036006414.8616.wf.mydomain.com,S=14076
> 8:2,

>     raise Errors.HeaderParseError(
> email.Errors.HeaderParseError: Not a header, not a continuation 

Dear David,

I've never seen a situation in which the email module raised that
error and it wasn't correct. You might want to look at the files in
question and see if it is.

I recall that there has been some discussion recently on the
mimelib-devel list (mimelib is the old name for the email module)
about what to do when that error would be raised or there are other
sorts of incorrect formatting. You can argue it both ways: On the one
hand, if common email software can read the message, people will
expect the email module to parse it. On the other hand, chasing
bug-for-bug compatibility with every possible RFC violation is a
fool's errand.

Of course, none of that sort of philosophizing will help your current
problem. First, I'd make sure that you're using version 2.4.3 of the
module. I think that it requires a relatively recent version of Python
and I don't think that there have been any recent changes that would
affect the problem you're seeing but it might be worth a try.

If the messages are formatted incorrectly and the most recent version
of the module can't parse them, you could subclass the module's Parser
and override its _parserheaders method with a less strict version. By
convention, you're not intended to override methods that begin with
underscores but you might not have much choice. You could also catch
the exception and do something about it. You might try using the
rfc822 module in that case (it may be less strict) as long as you
didn't mind not having the message's body parsed. You could also
rewrite the file to fix the formatting and run the Parser on it again.

BTW, many people consider it best not to post HTML to newsgroups.

Regards,
Matt



More information about the Python-list mailing list