Python 从 IMAP 帐户的消息中提取纯文本正文

发布于 2024-12-06 01:50:23 字数 464 浏览 0 评论 0原文

我一直在研究这个问题,但没有达到目标。

我可以通过 imaplib 连接并获取邮件。

msrv = imaplib.IMAP4(server)
msrv.login(username,password)

# Get mail

msrv.select()

#msrv.search(None, 'ALL')

typ, data = msrv.search(None, 'ALL')

# iterate through messages
for num in data[0].split():
    typ, msg_itm = msrv.fetch(num, '(RFC822)')
    print msg_itm
    print num 

但我需要做的是以纯文本形式获取消息正文,我认为这适用于电子邮件解析器,但我在使其工作时遇到问题。

有人有完整的例子我可以看一下吗?

谢谢,

I have been working on this and am missing the mark.

I am able to connect and get the mail via imaplib.

msrv = imaplib.IMAP4(server)
msrv.login(username,password)

# Get mail

msrv.select()

#msrv.search(None, 'ALL')

typ, data = msrv.search(None, 'ALL')

# iterate through messages
for num in data[0].split():
    typ, msg_itm = msrv.fetch(num, '(RFC822)')
    print msg_itm
    print num 

But what I need to do is get the body of the message as plain text and I think that works with the email parser but I am having problems getting it working.

Does anyone have a complete example I can look at?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£噩梦荏苒 2024-12-13 01:50:23

为了获取电子邮件正文的纯文本版本,我做了这样的事情......

xxx= data[0][1] #puts message from list into string


xyz=email.message_from_string(xxx)# converts string to instance of message xyz is an email message so multipart and walk work on it.

#Finds the plain text version of the body of the message.

if xyz.get_content_maintype() == 'multipart': #If message is multi part we only want the text version of the body, this walks the message and gets the body.
    for part in xyz.walk():       
        if part.get_content_type() == "text/plain":
            body = part.get_payload(decode=True)
        else:
                    continue

To get the plain text version of the body of the email I did something like this....

xxx= data[0][1] #puts message from list into string


xyz=email.message_from_string(xxx)# converts string to instance of message xyz is an email message so multipart and walk work on it.

#Finds the plain text version of the body of the message.

if xyz.get_content_maintype() == 'multipart': #If message is multi part we only want the text version of the body, this walks the message and gets the body.
    for part in xyz.walk():       
        if part.get_content_type() == "text/plain":
            body = part.get_payload(decode=True)
        else:
                    continue
谎言月老 2024-12-13 01:50:23

这是 docs 中的一个最小示例:

import getpass, imaplib

M = imaplib.IMAP4()
M.login(getpass.getuser(), getpass.getpass())
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
    print 'Message %s\n%s\n' % (num, data[0][1])
M.close()
M.logout()

在本例中, data[ 0][1]包含消息正文。

Here is a minimal example from the docs:

import getpass, imaplib

M = imaplib.IMAP4()
M.login(getpass.getuser(), getpass.getpass())
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
    print 'Message %s\n%s\n' % (num, data[0][1])
M.close()
M.logout()

In this case, data[0][1] contains the message body.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文