使用电子邮件模块(PYTHON)解析来自poplib的电子邮件内容 [英] parsing email contents from poplib with email module (PYTHON)

查看:211
本文介绍了使用电子邮件模块(PYTHON)解析来自poplib的电子邮件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PYTHON版本== 3.5

代码:

import getpass, poplib, email
Mailbox = poplib.POP3_SSL('pop.googlemail.com', '995')
Mailbox.user("email_here@gmail.com")
Mailbox.pass_('password_here')
numMessages = len(Mailbox.list()[1])
for i in range(numMessages):
    info  = b" ".join(Mailbox.retr(i+1)[1])
    msg = email.message_from_bytes(info)
    print(msg.keys())

输出:

['MIME-Version']
['MIME-Version']
['MIME-Version']
['Delivered-To']
['Delivered-To']
['Delivered-To']
['Delivered-To']
['Delivered-To']
['Delivered-To']
['Delivered-To']
['Delivered-To']

输出不正确,因为应该除了 MIME-Version
msg 中的其他字段已交付给
我认为

the output isn't correct because there should be more fields from the msg other than "MIME-Version" and "Delivered-To" I thought

em ail.message_from_bytes()解析字节字符串的内容

email.message_from_bytes() parses the contents of a byte string

不是 msg

文档建议这样做:

M = poplib.POP3('localhost')
M.user(getpass.getuser())
M.pass_(getpass.getpass())
numMessages = len(M.list()[1])
for i in range(numMessages):
    for j in M.retr(i+1)[1]:
        print(j)

是否可以使用电子邮件模块解析返回的消息?
,以便我们可以存储电子邮件详细信息。像发件人,正文,标头等。

Is there a way to parse the returned message using the email module? so we can store the email details. like sender, body, header etc.

推荐答案

答案很简单

import getpass, poplib, email
Mailbox = poplib.POP3_SSL('pop.googlemail.com', '995')
Mailbox.user("email_here@gmail.com")
Mailbox.pass_('password_here')
numMessages = len(Mailbox.list()[1])
for i in range(numMessages):
    raw_email  = b"\n".join(Mailbox.retr(i+1)[1])
    parsed_email = email.message_from_bytes(raw_email)
    print(parsed_email.keys())

而不是在 raw_email 中加入空格只需加入 \n 即可,电子邮件模块可以正确解析字段:

instead of joining raw_email with a space just join it by a \n and the email module can parse the fields correctly:

关于使用 email 模块
也是一件很棒的事情,当您调用 email.message_from_bytes( )返回的输出是
a dict

also an a awesome thing about using the email module is when you call email.message_from_bytes() the output returned is a dict

,因此您可以访问字段像这样:

so you access the fields like this:

raw_email  = b"\n".join(Mailbox.retr(i+1)[1])
parsed_email = email.message_from_bytes(raw_email)
print(parsed_email["header"])

但是如果该字段不存在怎么办?:

but what if the field doesn't exist?:

raw_email  = b"\n".join(Mailbox.retr(i+1)[1])
parsed_email = email.message_from_bytes(raw_email)
print(parsed_email["non-existent field"])

上面的代码将返回 None 且不抛出 KeyError

the above code will return None and not throw a KeyError

这篇关于使用电子邮件模块(PYTHON)解析来自poplib的电子邮件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆