种类繁多的python脚本,如果还不太清楚,抱歉。
我正在尝试保存一封电子邮件,其中包含HTML文件中的图像。我可以检索电子邮件,但是在保存时,内容似乎完全错误。
这是我正在使用的代码:
#!usr/bin/env python3
import imaplib
import email
import re
import getpass
import sys
from datetime import datetime, timedelta
user = input("Username: ")
pwd = getpass.getpass("Password: ")
mail = imaplib.IMAP4_SSL('outlook.office365.com')
try:
mail.login(user +"DOMAIN",pwd)
except (imaplib.IMAP4_SSL.error, imaplib.IMAP4.error) as err:
print('[ERROR] - Login failed!')
sys.exit()
mail.list()
mail.select("<FOLDER>")
yesterday = (datetime.now() - timedelta(1)).strftime("%Y_%m_%d")
report_time = yesterday
subject = (report_time + "WHATEVER")
# Begin of script
result, data = mail.search(None, '(SUBJECT "{0}")'.format(subject))
if data:
# data is a list.
ids = data[0]
# ids is a space separated string
if ids:
id_list = ids.split()
# Get the last one
latest_email_id = id_list[-1]
# Get the result and the data on the email (status + info)
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1].decode('utf-8')
# Getting the email content
body = email.message_from_string(raw_email)
if body.is_multipart():
for payload in body.get_payload():
if payload.get_content_type() == "text/html":
body = payload.get_payload(decode=True)
body = body.decode()
# This part is the issue I guess
f = open((result.html), 'wb')
f.write((' '.join(map(str, body)) + '\n').encode())
f.close()
我觉得我会将电子邮件“解码”到很多,这就是文件包含奇怪数据的原因。
因为,如果我打开result.html文件,它包含以下内容:
h t m l x m l n s:v =“ u r n:s c h e m a s-m i c r o s o f t-c o m:v m l
与屏幕上正在打印的body变量没有任何相似之处。