From b0aad911ab39fef1cb5c4ca626ae353bca326f18 Mon Sep 17 00:00:00 2001 From: Joseph Ishac Date: Mon, 7 Jun 2021 14:05:07 -0400 Subject: [PATCH] Adding some extra error handling when parsing from imaplib --- offlineimap/folder/IMAP.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py index 16d65da..05c977f 100644 --- a/offlineimap/folder/IMAP.py +++ b/offlineimap/folder/IMAP.py @@ -905,7 +905,20 @@ class IMAPFolder(BaseFolder): # Convert email, d[0][1], into a message object (from bytes) ndata0 = data[0][0].decode('utf-8') - ndata1 = self.parser['8bit-RFC'].parsebytes(data[0][1]) + try: ndata1 = self.parser['8bit-RFC'].parsebytes(data[0][1]) + except: + e = exc_info() + response_type = type(data[0][1]).__name__ + try: msg_id = \ + re.search(b"message-id:.*(<[A-Za-z0-9!#$%&'*+-/=?^_`{}|~.@ ]+>)", + re.split(b'[\r]?\n[\r]?\n', bytes(data[0][1]))[0], re.IGNORECASE).group(1) + except AttributeError: + # No match + msg_id = b"" + raise OfflineImapError( + "Exception parsing message ({} type {}) from imaplib.\n {}: {}".format( + msg_id, response_type, e[0].__name__, e[1]), + OfflineImapError.ERROR.MESSAGE) ndata = [ndata0, ndata1] return ndata