gpt4 book ai didi

Python poplib : retrieving body of the email result to NONE

转载 作者:太空宇宙 更新时间:2023-11-03 15:01:26 25 4
gpt4 key购买 nike

我可以获取电子邮件的主题,但正文始终结果为“无”。尝试按照此link但所有的建议最终都是一样的。正文仍然打印为 NONE。

import poplib
from email import parser
pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user('my_email.com')
pop_conn.pass_('my_password')
#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ['\n'.join(map(bytes.decode, mssg[1])) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]

for message in messages:
print (message['subject'])
print (message['body'])
print (message.get_payload())
pop_conn.quit()

最佳答案

import poplib
from email import parser
pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user('my_email.com')
pop_conn.pass_('my_password')
#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ['\n'.join(map(bytes.decode, mssg[1])) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]

for message in messages:
print (message['subject'])
print (message['from'])
for part in message.walk():
if part.get_content_type():
body = part.get_payload(decode=True)
print(body)
pop_conn.quit()

这成功了。然而它打印一条直线。有人知道如果消息正文很长如何每行打印吗?

关于Python poplib : retrieving body of the email result to NONE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com