TehPirate
TehPirate

Reputation: 164

IMAP command UID FETCH: Too long argument

I'm using imaplib (imap4) with Python 3.7.0 and I'm trying to fetch a large folder and I'm getting

FETCH command error: BAD [b'Error in IMAP command UID FETCH: Too long argument (0.001 + 0.122 + 0.122 secs).']

the code I'm using is below

    def run(self, verifyQueue):
        config = json.load(open("config.json", "r+"))
        client = IMAPClient(host=config.get('imapHost'))
        while True:
            try:
                client.login(config.get('imapUsername'), config.get('imapPassword'))
                client.select_folder(config.get('imapFolder'))
                self.log('Loaded')
                while True:
                    try:
                        client.idle()
                        responses = client.idle_check(timeout=30)
                        if responses:
                            client.idle_done()
                            messages = client.search("UNSEEN")
                            for uid, message_data in client.fetch(messages, "RFC822").items():
                                emailMSG = email.message_from_bytes(message_data[b"RFC822"])

This works perfectly for small folders but this folder has over a few thousand emails

Upvotes: 0

Views: 1272

Answers (1)

TehPirate
TehPirate

Reputation: 164

Splitting the messages seems to have solved the problem

messages = client.search("UNSEEN")
for i in range(0, len(messages), 500):
    for uid, message_data in client.fetch(messages[i:i + 500], "RFC822").items():

Upvotes: 1

Related Questions