我正在编写一个脚本,该脚本将能够从电子邮件中提取附件并将其作为新文件名保存到特定目录。由于某种原因,我一直在运行代码,并且从电子邮件模块中收到一条错误消息,指出对象不可下标。想知道是否有人可以帮助我解决这个问题。我知道错误的含义,但是对于电子邮件模块来说这是一个很新的知识,所以我真的无法弄清楚该怎么做才能克服它。
# Extracts Attachment and saves as new file
def get_attachment(msg):
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.getfilename()
if bool(fileName):
filePath = os.path.join(attachment_dir, fileName)
with open(filePath, 'wb') as file:
file.write(part.get_payload(decode=True))
os.rename(fileName, 'SpecialSituations_P&L ' + current_date() + '.png')
con = auth(username, password, imap_url)
#Select Email Folder
con.select('INBOX')
result, data = con.fetch(b'1440','(RFC822)')
raw = email.message_from_bytes(data[0][1])
get_attachment(raw)]
错误消息
File "extract_attachment_from_mail.py", line 45, in <module>
raw = email.message_from_bytes(data[0][1])
TypeError: 'NoneType' object is not subscriptable