[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Email messages from grouped email using IMAPClient in Python.

> On 12 Sep 2019, at 06:06, Srinivas Pullabhotla <nivas.p77 at gmail.com> wrote:
> Hello,
> I am trying to fetch email messages from a gmail inbox. So, there will be 1000s of messages sent to Inbox and since they are 1000s, the emails are grouped 100 per each email item.
> When I tried this method, the program only fetches some but not all and especially it happens with grouped email messages. How can I get all the messages from that grouped email.
> import email, time, sys
>    from imapclient import IMAPClient
>    with IMAPClient(HOST) as server:
>        server.login(USERNAME, PASSWORD)
>        server.select_folder('INBOX', readonly=True)
>        messages = server.search(['ALL', 'UNSEEN'])
>        for uid, message_data in server.fetch(messages, 'RFC822').items():
>            email_message = email.message_from_bytes(message_data[b'RFC822'])
>            print(email_message.get_payload(None, True))
> The program should fetch all the email messages from the grouped email and output to the file (in my case I am grabbing the href links). How best I can achieve this ? Appreciate thoughts and suggestions.

If you have a very large number of messages then you will need to add:

# default of 10000 is too small for SEARCH results
imaplib._MAXLINE = 200000

If you want to use the MOVE command you will need this:

# MOVE is not available by default
imaplib.Commands['MOVE'] = ('SELECTED',)

As DL said you need to print out what you get back from the search() and check that it is what you expect.


> -- 
> https://mail.python.org/mailman/listinfo/python-list