Current Problem
I've pieced together a script which downloads attachments from a mailbox in gmail and for the most part pulls a list of variables based on the email the information is pulled from. However I found that in some cases the "Message ID" of an email could be listed as "Message ID" or "Message-ID". Because of this I've tried to use regex to take into account that there could be anything between "Message" and "ID" but my code spits out errors regardless of what I have tried to so far with the expression.
Error
> Traceback (most recent call last): File "email-downloader.py", line
> 64, in <module>
> msg_id = str(email_message).split("Message+\.*: ", 1)[1].split("\n", 1)[0] IndexError: list index out of range
What I've tried
I've looked online and wasn't able to find an answer in the past which was able to resolve this issue. I also tried to amend the regex with different + placements or the use of \ and []
Code
email_from = str(email_message).split("From: ", 1)[1].split("\n", 1)[0]
subject = str(email_message).split("Subject: ", 1)[1].split("\n", 1)[0]
ext = os.path.splitext(fileName)[1]
delivered = str(email_message).split("Date: ", 1)[1].split("\n", 1)[0]
msg_id = str(email_message).split("Message+\.*: ", 1)[1].split("\n", 1)[0]
print('File: "{file}".'.format(file=fileName))
print('Ext: "{ext}".'.format(ext=ext))
print('Subject: "{subject}".'.format(subject=subject))
print('From: "{email_from}".'.format(email_from=email_from))
print('Date Delivered: "{delivered}".'.format(delivered=delivered))
print('Message ID: "{msg_id}".'.format(msg_id=msg_id))
print("\n")
print('"{msg_id}" "{delivered}" "{file}" "{subject}" "{email_from}"'.format(file=fileName,subject=subject,email_from=email_from,msg_id=msg_id,delivered=delivered), file=open("array/client-ref.tsv", "a"))
os.rename(os.path.join(dirName,fileName), os.path.join(dirName,msg_id + ext))