Skip to content

Commit aa0b831

Browse files
committed
Handle splitted structures when fetching data. Output a warning message instead of raising IMAP4 errors
1 parent bbf6443 commit aa0b831

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

imap_aex.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
-v --verbose Display more information.
5252
"""
5353
import calendar
54+
import imaplib
5455
import sys
5556
from binascii import Error as BinasciiError
5657
import os
@@ -343,7 +344,25 @@ def extract(self, date_def=None, fetch_all=False):
343344
if status != "OK":
344345
print("Could not fetch messages.")
345346

347+
merge_previous = False
348+
previous_structure = b''
349+
346350
for structure in fetch_data: # type: bytes
351+
if type(structure) in (list, tuple):
352+
if len(structure) == 2:
353+
structure = structure[0] + b'"'+structure[1]+b'"'
354+
else:
355+
structure = b' '.join(structure)
356+
357+
if not structure.endswith(b')'):
358+
previous_structure = b''+structure
359+
merge_previous = True
360+
continue
361+
362+
if merge_previous:
363+
structure = previous_structure + structure
364+
merge_previous = False
365+
347366
reg_attachment = "attachment|application"
348367
if self.inline_images:
349368
reg_attachment = reg_attachment+"|image"
@@ -356,6 +375,7 @@ def extract(self, date_def=None, fetch_all=False):
356375
if not has_attachments:
357376
continue
358377
uid = structure.split(b' ')[0]
378+
359379
if uid:
360380
to_fetch.append(uid)
361381
else:
@@ -369,7 +389,12 @@ def extract(self, date_def=None, fetch_all=False):
369389
exit(0)
370390

371391
for uid in to_fetch:
372-
status, fetch_data = self.imap.fetch(uid, '(FLAGS RFC822)')
392+
try:
393+
status, fetch_data = self.imap.fetch(uid, '(FLAGS RFC822)')
394+
except imaplib.IMAP4.error as e:
395+
print("Encountered error when reading mail uid %s: %s" % (uid, repr(e)))
396+
continue
397+
373398
if status != "OK":
374399
print("Could not fetch messages")
375400

0 commit comments

Comments
 (0)