我想知道是否可以下载您可以在电子邮件中看到的.eml附件(已删除)。 我试图做这个代码,但只有当你从本地附加文件时才有效,而不是当你把电子邮件拖到其他地方发送它时(我希望你明白我想要的)
代码:
imaplib.IMAP4.debug = imaplib.IMAP4_SSL.debug = 1
con = imaplib.IMAP4_SSL('outlook.office365.com')
con.login(username, password)
con.select('INBOX')
typ, data = con.search(None, '(UNSEEN)')
c1 = 0
data_list = []
unread_msg_nums = data[0].split()
for num in unread_msg_nums:
data_dict = {}
typ, data = con.fetch(num, '(BODY.PEEK[])')
c1 +=1
text = data[0][1]
msg = email.message_from_string(text.decode('utf-8'))
data_dict['mail_to'] = msg['To']
data_dict['mail_subject'] = msg['Subject']
data_dict['mail_from'] = email.utils.parseaddr(msg['From'])
data_dict['body'] = msg.get_payload()
data_list.append(data_dict)
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
data = part.get_payload(decode=True)
if not data or not filename:
continue
with io.FileIO(my_path+'/'+filename, "w") as file:
file.write(data)
con.close()
con.logout()