该应用程序的想法是充当电子邮件客户端,因此无论我从电子邮件服务提供商那里得到什么,我都应该能够在应用程序中显示它。该应用程序是使用Flask使用IMAP编写的,以从电子邮件服务提供商(例如Gmail或Outlook)中检索电子邮件。不幸的是,接收到的HTML存在问题。每个等号后都有3D
个字符。我唯一想到的是检索HTML的方法存在问题。以下是我用来检索HTML的代码。
class EmailClient:
imap_host = 'imap.gmail.com'
imap_user = 'test@gmail.com'
imap_pass = 'test'
def process_mailbox(M):
diction = []
rv, data = M.search(None, "ALL")
if rv != 'OK':
print('No messages found!')
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print("ERROR getting message", num)
return
msg = email.message_from_bytes(data[0][1])
hdr = email.header.make_header(email.header.decode_header(msg['Subject']))
subject = str(hdr)
print('Message %s: %s' % (num, subject))
date_tuple = email.utils.parsedate_tz(msg['Date'])
if date_tuple:
local_date = datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple))
print('Local Date:', local_date.strftime('%a, %d %b %Y %H:%M:%S'))
for part in msg.walk():
if part.get_content_type() == 'text/html':
print(part.get_payload())
diction.append(part.get_payload())
M = imaplib.IMAP4_SSL('imap.gmail.com')
try:
rv, data = M.login(imap_user, imap_pass)
except imaplib.IMAP4.error:
print("LOGIN FAILED!")
sys.exit(1)
print(rv, data)
rv, mailboxes = M.list()
if rv == 'OK':
print('Mailboxes:')
print(mailboxes)
rv, data = M.select('Inbox')
if rv == 'OK':
print('Processing mailbox...\n')
process_mailbox(M)
M.close()
else:
print('ERROR: Unable to open mailbox', rv)
M.logout()
以下是我正在获取的HTML的摘要:
<img width=3D"134" height=3D"46" border=3D"0"