ItGo.me Focus on IT Recommend

Home > parsing email contents from poplib with email module (PYTHON)

parsing email contents from poplib with email module (PYTHON)

2020腾讯云8月秒杀活动,优惠非常大!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1040

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

PYTHON VERSION == 3.5

code:

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())

output:

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

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

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

is msg not a byte string?

the docs recommend this:

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.

python email python-3.x pop3
|
  this question
asked Feb 28 '16 at 6:04 halcyonjuly7 52 7

 | 

1 Answers
1

the answer turned out to be fairly easy

Recommend:parsing - python parser for liquid email templating

m/Shopify/liquid If not is there is any smart way to integrate available parser with Python. Thanks in advance. python parsing liquid templating email-templates
  |
  this question asked Apr 20 '16 at 5:55 Dheeraj Pande 13 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):
    raw_email  = b"\n".join(Mailbox.retr(i+1)[1])
    parsed_email = email.message_from_bytes(raw_email)
    print(parsed_email.keys())

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

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"])

the above code will return None and not throw a KeyError


|
  this answer
answered Feb 28 '16 at 18:38 halcyonjuly7 52 7

 | 

Recommend:python - Parsing date with timezone from an email

arser().parse(file)date = message['Date']print date and I receive: 'Mon, 16 Nov 2009 13:32:02 +0100' But I need a nice datetime object, so I use: datetime.strptime('Mon, 16 Nov 2009 13:32:02 +0100', '%a, %d %b %Y %H:%M:%S %Z') which ra

oriUlr:http://stackoverflow.com/questions/35679338/parsing-email-contents-from-poplib-with-email-module-python

------splitte line----------------------------