Skip to content

Commit

Permalink
Handle splitted structures when fetching data. Output a warning messa…
Browse files Browse the repository at this point in the history
…ge instead of raising IMAP4 errors
  • Loading branch information
Danamir committed Sep 16, 2021
1 parent bbf6443 commit aa0b831
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion imap_aex.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
-v --verbose Display more information.
"""
import calendar
import imaplib
import sys
from binascii import Error as BinasciiError
import os
Expand Down Expand Up @@ -343,7 +344,25 @@ def extract(self, date_def=None, fetch_all=False):
if status != "OK":
print("Could not fetch messages.")

merge_previous = False
previous_structure = b''

for structure in fetch_data: # type: bytes
if type(structure) in (list, tuple):
if len(structure) == 2:
structure = structure[0] + b'"'+structure[1]+b'"'
else:
structure = b' '.join(structure)

if not structure.endswith(b')'):
previous_structure = b''+structure
merge_previous = True
continue

if merge_previous:
structure = previous_structure + structure
merge_previous = False

reg_attachment = "attachment|application"
if self.inline_images:
reg_attachment = reg_attachment+"|image"
Expand All @@ -356,6 +375,7 @@ def extract(self, date_def=None, fetch_all=False):
if not has_attachments:
continue
uid = structure.split(b' ')[0]

if uid:
to_fetch.append(uid)
else:
Expand All @@ -369,7 +389,12 @@ def extract(self, date_def=None, fetch_all=False):
exit(0)

for uid in to_fetch:
status, fetch_data = self.imap.fetch(uid, '(FLAGS RFC822)')
try:
status, fetch_data = self.imap.fetch(uid, '(FLAGS RFC822)')
except imaplib.IMAP4.error as e:
print("Encountered error when reading mail uid %s: %s" % (uid, repr(e)))
continue

if status != "OK":
print("Could not fetch messages")

Expand Down

0 comments on commit aa0b831

Please sign in to comment.