Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using email.HeaderParser with imaplib.fetch in python?

Tags:

python

email

Does anyone have a good example of using the HeaderParser class in Python for a message that you pull down with imaplib.fetch?

I have been able to find a lot of related things, but nothing that does just this.

Do I need to full down the fetch has an RFC822? I was hoping to simply pull down the subjects.

Thanks!

like image 265
Hortitude Avatar asked Mar 31 '09 21:03

Hortitude


1 Answers

Good news: you're right... you don't need to pull down the RFC822. The message_parts parameter to fetch() lets you be quite fine-grained.

Here's a simple example of how to fetch just the header:

import imaplib
from email.parser import HeaderParser

conn = imaplib.IMAP4('my.host.com')
conn.login('[email protected]', 'mypassword')
conn.select()
conn.search(None, 'ALL') # returns a nice list of messages...
                         # let's say I pick #1 from this

data = conn.fetch(1, '(BODY[HEADER])')

# gloss over data structure of return... I assume you know these
# gives something like:
# ('OK', [(1 (BODY[HEADER] {1662', 'Received: etc....')])
header_data = data[1][0][1]

parser = HeaderParser()
msg = parser.parsestr(header_data)
<email.message.Message instance at 0x2a>

print msg.keys()
['Received', 'Received', 'Received', 'Cc', 'Message-Id', 'From', 'To',
'In-Reply-To', 'Content-Type', 'Content-Transfer-Encoding', 'Mime-Version',
'Subject', 'Date', 'References', 'X-Mailer', 
'X-yoursite-MailScanner-Information',
'X-yoursite-MailScanner', 'X-yoursite-MailScanner-From', 'Return-Path',
'X-OriginalArrivalTime']

The full list of message parts that can be passed as the second argument to fetch is in the IMAP4 spec: https://www.rfc-editor.org/rfc/rfc1730#section-6.4.5

like image 96
Jarret Hardie Avatar answered Oct 17 '22 06:10

Jarret Hardie