I'm using IMAPclient. After I return an imapclient object I pass it to the function below together with the id of the email and a string of the label I want the email to be labeled with. This works in general but I get this error in a few instances.
Function called when adding a label
def label_gmail_message(client, msgid, label):
"""Function to label gmail messages after parsing
Args:
client (obj): imap client object to mail server
msgid (str): id of message to label
label (str): label string to be used
"""
try:
client.add_gmail_labels(msgid, label)
except KeyError:
log.exception(f"KeyError when labelling message : {msgid} with label {label}")
Error thrown
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/analisa-0.1.0-py3.7.egg/email_parser/imap_client.py", line 359, in label_gmail_message
client.add_gmail_labels(msgid, label)
File "/usr/local/lib/python3.7/site-packages/imapclient/imapclient.py", line 1034, in add_gmail_labels
silent=silent)
File "/usr/local/lib/python3.7/site-packages/imapclient/imapclient.py", line 1370, in _gm_label_store
b'X-GM-LABELS', silent=silent)
File "/usr/local/lib/python3.7/site-packages/imapclient/imapclient.py", line 1392, in _store
fetch_key)
File "/usr/local/lib/python3.7/site-packages/imapclient/imapclient.py", line 1396, in _filter_fetch_dict
for msgid, data in iteritems(fetch_dict))
File "/usr/local/lib/python3.7/site-packages/imapclient/imapclient.py", line 1396, in <genexpr>
for msgid, data in iteritems(fetch_dict))
KeyError: b'X-GM-LABELS'