Handling emails

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jun 12 07:45:49 EDT 2011


On Sun, 12 Jun 2011 19:20:00 +0800, TheSaint wrote:

> Hello
> I wrote a program which was working on python 2.x. I'd like to go for
> newer version but I face the problem on how the emails are parsed. In
> particular I'd like to extract the significant parts of the headers, but
> the query to the servers had turned in to list of bytes. What could be a
> method that will parse and return the headers into ascii if I'll pass
> the headers as bytes. Even I don't know whether I can pass as they
> arrive to the program.
> 
> For example if I try:
> 
> import poplib.POP3
> _pop= poplib.POP3(srvr)
> _pop.user(args[1])
> _pop.pass_(args[2])
> 
> header =_pop.top(nmuid, 0)
> 
> This will return a list of bytes string and I don't have idea to process
> them in order to have a dictionary containing 'from', 'to', 'cc', 'bcc',
> 'date', 'subject', 'reply-to', 'message-id' as keys.

To parse emails, you should use the email package. It already handles 
bytes and strings.

Other than that, I'm not entirely sure I understand your problem. In 
general, if you have some bytes, you can decode it into a string by hand:

>>> header = b'To: python-list at python.org\n'
>>> s = header.decode('ascii')
>>> s
'To: python-list at python.org\n'


If this is not what you mean, perhaps you should give an example of what 
header looks like, what you hope to get, and a concrete example of how it 
differs in Python 3.


-- 
Steven



More information about the Python-list mailing list