Fetching multiple IMAP messages at once
Asked Answered
N

2

7

The examples I've seen about loading emails over IMAP using python do a search and then for each message id in the results, do a query. I want to speed things up by fetching them all at once.

Neocene answered 27/8, 2010 at 5:52 Comment(1)
I want to do a search and then fetch all resulting messages in one operation. Unless someone added a new physics law when I wasn't paying attention, I think I'll be alright.Neocene
N
16

RFC 3501 says fetch takes a sequence set, but I didn't see a definition for that and the example uses a range form (2:4 = messages 2, 3, and 4). I figured out that a comma separated list of ids works. In python with imaplib, I've got something like:

    status, email_ids = con.search(None, query)
    if status != 'OK':
        raise Exception("Error running imap search for spinvox messages: "
                        "%s" % status)

    fetch_ids = ','.join(email_ids[0].split())
    status, data = con.fetch(fetch_ids, '(RFC822.HEADER BODY.PEEK[1])')
    if status != 'OK':
        raise Exception("Error running imap fetch for spinvox message: "
                        "%s" % status)
    for i in range(len(email_ids[0].split())):
        header_msg = email.message_from_string(data[i * 3 + 0][1])
        subject = header_msg['Subject'],
        date = header_msg['Date'],
        body = data[i * 3 + 1][1] # includes some mime multipart junk
Neocene answered 27/8, 2010 at 5:57 Comment(0)
E
4

You can try this to fetch the header information of all the mails in just 1 Go to server.

import imaplib
import email

obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)
obj.login('username', 'password')
obj.select('folder_name')
resp,data = obj.uid('FETCH', '1:*' , '(RFC822.HEADER)')
messages = [data[i][1].strip() + "\r\nSize:" + data[i][0].split()[4] + "\r\nUID:" + data[i][0].split()[2]  for i in xrange(0, len(data), 2)]
for msg in messages:
    msg_str = email.message_from_string(msg)
    message_id = msg_str.get('Message-ID')
Exhibitioner answered 27/9, 2010 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.