David Neudorfer
David Neudorfer

Reputation: 15

IMAPClient and BODY[HEADER.FIELDS (FROM)] field

I'm really starting to get the hang of IMAPClient. The code: 'BODY[HEADER.FIELDS (FROM)]' returns

From: First Last <[email protected]>

I'd really just like it to return the email address like this:

[email protected]

Do I need to pass it to a variable first and trim it down or is there another fetch modifier I can use?

response = server.fetch(messages, ['FLAGS', 'RFC822.SIZE', 'BODY[HEADER.FIELDS (FROM)]'])
for msgid, data in response.iteritems():
    print '   ID %d: %d bytes, From: %s flags=%s' % (msgid,
                                            data['RFC822.SIZE'],
                                            data['BODY[HEADER.FIELDS (FROM)]'],
                                            data['FLAGS'])

Upvotes: 1

Views: 2474

Answers (2)

Max
Max

Reputation: 10985

IMAPLIB doesn't parse much of the protocol for you. It's returning the line from the server as is.

You can and should use the parsers in the email library to help you out.

Upvotes: 0

Jon Clements
Jon Clements

Reputation: 142206

No - you can't do that with an IMAP request, if you look at my other post you'll notice something using parseaddr, but here it is again with your example:

>>> from email.utils import parseaddr
>>> a = 'From: First Last <[email protected]>'
>>> parseaddr(a)
('First Last', '[email protected]')

Upvotes: 3

Related Questions