PYTHON VERSION == 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']
输出不正确,因为应该有更多的字段
除msg
和"MIME-Version"
以外的"Delivered-To"
我认为
email.message_from_bytes()
解析字节字符串的内容
msg
不是字节字符串吗?
docs推荐这个:
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)
有没有办法使用电子邮件模块解析返回的邮件? 所以我们可以存储电子邮件详细信息像发件人,身体,标题等。
答案 0 :(得分:1)
答案结果相当容易
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
加入,email
模块就可以正确解析字段:
使用email
模块也是一个很棒的事情
是当你调用email.message_from_bytes()
时返回的输出是
一个dict
所以你可以访问这样的字段:
raw_email = b"\n".join(Mailbox.retr(i+1)[1])
parsed_email = email.message_from_bytes(raw_email)
print(parsed_email["header"])
但如果该字段不存在会怎么样?:
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