如何使用python imaplib阅读电子邮件

我创建了一个可以读取电子邮件并向用户显示其内容的电子邮件系统。

我拥有的代码无法正常显示内容。我知道有些电子邮件使用html格式,所以想知道是否可能是这种情况。无论哪种方式,我都需要帮助以使其具有可读格式,最好看起来像原始格式。

我得到的输出

From: Google Community Team <googlecommunityteam-noreply@google.com>
Subject: Finish setting up your new Google Account
Content: R29vZ2xlICANCjxodHRwczovL3d3dy5nb29nbGUuY29tL2FwcHNlcnZlL21rdC9wL0FELUZuRXhO
cjkzR21VVWhrSXg1NlQtNl9pM0xpMzRvT3hHeXZYX3BfUW9uR2QzWndYTGw1VTl0cnV5bHJ2ZnNS
QnEzLVJ1ck44eGlWN2ZXeG5FM0dYZ1pvR3ozQURlNGpJSG5BZEtZcE5uRFpSVGdyVzV0QXBZY1d4
WXhibHlKLUxHcVVucFRpVUlZS2g4WTg4YmZQWVB1eUJoMmZHRGM+DQoNCmJhbm5lcg0KDQpIaSBE
YW5pZWwsDQoNCldlbGNvbWUgdG8gR29vZ2xlLiBZb3VyIG5ldyBhY2NvdW50IGNvbWVzIHdpdGgg
YWNjZXNzIHRvIEdvb2dsZSBwcm9kdWN0cywgIA0KYXBwcywgYW5kIHNlcnZpY2VzLg0KDQpIZXJl
IGFyZSBhIGZldyB0aXBzIHRvIGdldCB5b3Ugc3RhcnRlZC4NCg0KU2VjdXJpdHkNCg0KQ29udHJv
bCB5b3VyIGFjY291bnQNCg0KQ2hvb3NlIHdoYXQncyByaWdodCBmb3IgeW91LiBSZXZpZXcgYW5k
IGFkanVzdCB5b3VyIHByaXZhY3kgYW5kIHNlY3VyaXR5ICANCnNldHRpbmdzIGFueSB0aW1lLg0K
DQpHb29nbGUgQWNjb3VudCAgDQo8aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9hcHBzZXJ2ZS9ta3Qv
cC9BRC1GbkV4T2JYQURQdzJVZmtqSGxBX21lcEQ1cEpFTU1DOGtya1BPUEJwYmtFMU1ZamdyMGFa
SmRTSk1ncHhmS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lZC4gSWYgeW91IGhhdmUgYSBxdWVzdGlvbiBhYm91dCB5b3VyICANCm5l
dyBhY2NvdW50LCB0aGUgSGVscCBDZW50ZXIgIA0KPGh0dHBzOi8vd3d3Lmdvb2dsZS5jb20vYXBw
c2VydmUvbWt0L3AvQUQtRm5Fd0xtMUdQLUtEZUZrUTRId2libnBPMWdsZzRtQXMxYXBEalJMeG1B
elM4My16NEhrajJZYjJ6RUlIelltTnpzTTJJdEJOWUtfMkVzQ1BraVI3UXdLbXM4djdFcllLTTEy
b2pDRmlRQ3BURUoxWkp3NU1WekwzcWlCSHo5dXZtUUR0alhnY2JGQjV1LVotME1TdXdHZUxEOU1x
WjQxQVY0b0NkSXFyWlRmMkpYaEtIZ0VZZWF3PiAgDQpsaWtlbHkgaGFzIHRoZSBhbnN3ZXIgeW91
J3JlIGxvb2tpbmcgZm9yLg0KDQoNCg0KR29vZ2xlIExMQyAxNjAwIEFtcGhpdGhlYXRyZSBQYXJr
d2F5LCBNb3VudGFpbiBWaWV3LCBDQSA5NDA0Mw0KVGhpcyBlbWFpbCB3YXMgc2VudCB0byB5b3Ug
YmVjYXVzZSB5b3UgY3JlYXRlZCBhIEdvb2dsZSBhY2NvdW50Lg0K

我的代码是:

import email
import imaplib

mail = imaplib.IMAP4_SSL(SERVER)
mail.login(EMAIL, PASSWORD)
mail.select('inbox')


status, data = mail.search(None, 'ALL')

mail_ids = []

for block in data:
    mail_ids += block.split()

for i in mail_ids:
    status, data = mail.fetch(i, '(RFC822)')

    for response_part in data:
        if isinstance(response_part, tuple):
            message = email.message_from_bytes(response_part[1])

            mail_from = message['from']
            mail_subject = message['subject']

            if message.is_multipart():
                mail_content = ''
                for part in message.get_payload():
                    if part.get_content_type() == 'text/plain':
                        mail_content += part.get_payload()
            else:
                mail_content = message.get_payload()

            print(f'From: {mail_from}')
            print(f'Subject: {mail_subject}')
            print(f'Content: {mail_content}')
com'

mail = imaplib.IMAP4_SSL(SERVER)
mail.login(EMAIL, PASSWORD)
mail.select('inbox')


status, data = mail.search(None, 'ALL')

mail_ids = []

for block in data:
    mail_ids += block.split()

for i in mail_ids:
    status, data = mail.fetch(i, '(RFC822)')

    for response_part in data:
        if isinstance(response_part, tuple):
            message = email.message_from_bytes(response_part[1])

            mail_from = message['from']
            mail_subject = message['subject']

            if message.is_multipart():
                mail_content = ''
                for part in message.get_payload():
                    if part.get_content_type() == 'text/plain':
                        mail_content += part.get_payload()
            else:
                mail_content = message.get_payload()

            print(f'From: {mail_from}')
            print(f'Subject: {mail_subject}')
            print(f'Content: {mail_content}')

那么有什么方法可以使格式变得可读?

评论
  • 离心咒
    离心咒 回复

    内容(在这种情况下)是base64编码的。你会想要

    1. 确定内容的编码,因为它可能使用不同的编码,
    2. 导入适当的模块,例如base64或uu,然后,
    3. 从模块对编码内容调用解码功能。

    对于#1,完整的邮件内容中可能还有另一个标头,指示使用哪种编码。