使用电子邮件库读取来自 Outlook 的电子邮件回复 python

read email response from outlook using email lib python

您好,我目前正在使用 python 的 IMAPLIB 和 EMAIL 库进行阅读,然后获取不同电子邮件的内容发送,但是当我收到来自 Outlook 的电子邮件回复时,我无法从电子邮件中获取内容我收到这样的字符串:

'PGh0bWw+DQo8aGVhZD4NCjxtZXRhIGh0dHAtZXF1aXY9IkNvbnRlbnQtVHlwZSIgY29udGVudD0i\r\ndGV4dC9odG1sOyBjaGFyc2V0PXV0Zi04Ij4NCjxzdHlsZSB0eXBlPSJ0ZXh0L2NzcyIgc3R5bGU9\r\nImRpc3BsYXk6bm9uZTsiPiBQIHttYXJnaW4tdG.......'

这是获取电子邮件的代码,我用它来验证 IMAPLIB,电子邮件服务器是用这个 repo 构建的:https://github.com/modoboa/modoboa

mail.select("inbox")
result, data = mail.uid('search', None, 'UNSEEN')

inbox_item_list = data[0].split()
list_emails = []
root_dict = {}
campaigns = []


 for item in inbox_item_list:
    result2, email_data = mail.uid('fetch', item, '(RFC2045)')
    raw_email = email_data[0][1].decode('utf-8')
    email_message = email.message_from_string(raw_email)
    to_= email_message["Delivered-To"]
    from_= email_message['From']
    subject_= email_message['Subject']
    counter = 1
    for part in email_message.walk():
        if part.get_content_maintype() == "multipart":
            continue
        filename = part.get_filename()
        if not filename:
            ext = 'html'
            filename = 'msg-part-%08d%s' %(counter, ext)
        counter += 1
  
    content_type = part.get_content_type()

    if "plain" in content_type:
        pass
    elif "html" in content_type:
        emails = {}
        html_ = part.get_payload()
        soup_ = BeautifulSoup(html_, "html.parser")
        text = soup_.get_text()

text 是保存电子邮件内容的变量,适用于 Gmail,但目前不适用于 outlook 的电子邮件回复。

原来它只是 base64 编码的。这是解码后的文本:

b'<html>\r\n<head>\r\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\r\n<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>\r\n</head>\r\n<body dir="ltr">\r\n<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">\r\nCount on me<br>\r\n</div>\r\n<div id="appendonsend"></div>\r\n<hr style="display:inline-block;width:98%" tabindex="-1">\r\n<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>De:</b> Giraluna Gomez &lt;2344@holbertonschool.com&gt;<br>\r\n<b>Enviado:</b> lunes, 24 de mayo de 2021 4:13 p. m.<br>\r\n<b>Para:</b> Giraluna Gomez &lt;lunagolo@hotmail.com&gt;<br>\r\n<b>Cc:</b> david+2022@zeroclickmail.com &lt;david+2022@zeroclickmail.com&gt;<br>\r\n<b>Asunto:</b> Luna golo.Test 2 from email merge</font>\r\n<div>&nbsp;</div>\r\n</div>\r\n<div>\r\n<div dir="ltr">Test 2 to email merge.&nbsp;</div>\r\n<div hspace="streak-pt-mark" style="max-height:1px"><img alt="" style="width:0px; max-height:0px; overflow:hidden" src="https://mailfoogae.appspot.com/t?sender=aMjM0NEBob2xiZXJ0b25zY2hvb2wuY29t&amp;type=zerocontent&amp;guid=9189324e-7d1c-4590-b502-8179902f97de"><font color="#ffffff" size="1">\xe1\x90\xa7</font></div>\r\n</div>\r\n</body>\r\n</html>\r\n'

下面是我用来解码的内容:

from base64 import b64decode

print(b64decode("PGh0bWw+DQo8aGVhZD4NCjxtZXRhIGh0dHAtZXF1aXY9IkNvbnRlbnQtVHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PXV0Zi04Ij4NCjxzdHlsZSB0eXBlPSJ0ZXh0L2NzcyIgc3R5bGU9ImRpc3BsYXk6bm9uZTsiPiBQIHttYXJnaW4tdG9wOjA7bWFyZ2luLWJvdHRvbTowO30gPC9zdHlsZT4NCjwvaGVhZD4NCjxib2R5IGRpcj0ibHRyIj4NCjxkaXYgc3R5bGU9ImZvbnQtZmFtaWx5OiBDYWxpYnJpLCBIZWx2ZXRpY2EsIHNhbnMtc2VyaWY7IGZvbnQtc2l6ZTogMTJwdDsgY29sb3I6IHJn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3BvdC5jb20vdD9zZW5kZXI9YU1qTTBORUJvYjJ4aVpYSjBiMjV6WTJodmIyd3VZMjl0JmFtcDt0eXBlPXplcm9jb250ZW50JmFtcDtndWlkPTkxODkzMjRlLTdkMWMtNDU5MC1iNTAyLTgxNzk5MDJmOTdkZSI+PGZvbnQgY29sb3I9IiNmZmZmZmYiIHNpemU9IjEiPuGQpzwvZm9udD48L2Rpdj4NCjwvZGl2Pg0KPC9ib2R5Pg0KPC9odG1sPg0K"))

如果您只想使用 mailbox/emails(不阅读 RFC 和解析数据),请尝试:

https://github.com/ikvk/imap_tools:

from imap_tools import MailBox, A

with MailBox('imap.mail.com').login('test@mail.com', 'pwd', 'INBOX') as mailbox:
    for msg in mailbox.fetch(A(all=True)):
        body = msg.text or msg.html

*此致,imap_tools

的作者