By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,533 Members | 1,334 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,533 IT Pros & Developers. It's quick & easy.

Using poplib to parse headers

P: n/a
Hello,

I am writing a Python program to check email using POP3. I've tried
the sample code from python.org, and it works great. In other words,
the code below successfully prints out my emails.

import getpass, poplib, email
M = poplib.POP3('mail.blah.com')
M.user('username')
M.pass_('password')
numMessages = len(M.list()[1])
for i in range(numMessages):
for j in M.retr(i+1)[1]:
print j
M.quit()

However, if I understand right, the Python poplib library will also
parse the email for me so that I can iterate through the headers,
body, etc, of each message, and use them in my program. I think the
method I need to use is email.message_from_file, but I'm having
difficulty getting it to work. Can anyone explain me how I would
extend the above example to do this?

I tried to do this by using in the i loop the line:

message = email.message_from_file(j)

but I get the error: "AttributeError: 'str' object has no attribute 'readline'"

Please forgive this very basic question. I do most of my programming
in PHP and I'm just getting started with Python.

Many thanks,

J-C

Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On May 27, 8:39 am, Jean-Claude Neveu <jcn-france1...@pobox.com>
wrote:
I tried to do this by using in the i loop the line:

message = email.message_from_file(j)

but I get the error: "AttributeError: 'str' object has no attribute 'readline'"
Heya,

The email module has a second function - 'message_from_string' - which
might be more useful here. Currently, you're passing to
'message_from_file' a string that it tries to treat as a file, hence
it complaining about the string object not having part of the file
object interface.

(I've never used email or poplib, so this is just a guess...)

- alex23
Jun 27 '08 #2

P: n/a
On May 27, 12:39 am, Jean-Claude Neveu <jcn-france1...@pobox.com>
wrote:
Hello,

I am writing a Python program to check email using POP3. I've tried
the sample code from python.org, and it works great. In other words,
the code below successfully prints out my emails.

import getpass, poplib, email
M = poplib.POP3('mail.blah.com')
M.user('username')
M.pass_('password')
numMessages = len(M.list()[1])
for i in range(numMessages):
for j in M.retr(i+1)[1]:
print j
M.quit()

However, if I understand right, the Python poplib library will also
parse the email for me so that I can iterate through the headers,
body, etc, of each message, and use them in my program. I think the
method I need to use is email.message_from_file, but I'm having
difficulty getting it to work. Can anyone explain me how I would
extend the above example to do this?

I tried to do this by using in the i loop the line:

message = email.message_from_file(j)

but I get the error: "AttributeError: 'str' object has no attribute 'readline'"

Please forgive this very basic question. I do most of my programming
in PHP and I'm just getting started with Python.

Many thanks,

J-C
Here's something I wrote when I was learning Python:

http://gflanagan.net/site/python/pag...liacci.py.html

HTH (if you can decipher it!)

Gerard
Jun 27 '08 #3

P: n/a
On 06:39, martedì 27 maggio 2008 Jean-Claude Neveu wrote:
However, if I understand right, the Python poplib library will also
parse the email for me so that I can iterate through the headers
See my program at http://it.geocities.com/call_me_not_now/index.html
You may study it.

But for short you can use

from email.Parser import HeaderParser
# then you can parse the mail, body etc here below

M = poplib.POP3('mail.blah.com')
M.user('username')
M.pass_('password')
numMsgs = len(M.list()[1])
for cnt in range(1, numMsgs +1):
header= M.top(cnt,0)[1])

HTH
Jun 27 '08 #4

P: n/a
Jean-Claude Neveu <jc************@pobox.comwrote:
>
I am writing a Python program to check email using POP3. I've tried
the sample code from python.org, and it works great. In other words,
the code below successfully prints out my emails.

import getpass, poplib, email
M = poplib.POP3('mail.blah.com')
M.user('username')
M.pass_('password')
numMessages = len(M.list()[1])
for i in range(numMessages):
for j in M.retr(i+1)[1]:
print j
M.quit()

However, if I understand right, the Python poplib library will also
parse the email for me so that I can iterate through the headers,
body, etc, of each message, and use them in my program. I think the
method I need to use is email.message_from_file, but I'm having
difficulty getting it to work. Can anyone explain me how I would
extend the above example to do this?

I tried to do this by using in the i loop the line:

message = email.message_from_file(j)

but I get the error: "AttributeError: 'str' object has no attribute 'readline'"
You've received some very confusing advice in this thread. Alex had the
right answer, but I want to expand it a bit.

You said "the Python poplib library will also parse the email for me". This
is incorrect. poplib, like many of the modules of the Python standard
library, focuses on exactly one purpose: handling the POP3 protocol. It
will allow you to count your messages, and fetch your messages, but that's
it, because that's all that POP3 does. It's a very simple protocol.

Now, the standard library DOES include modules for parsing email, as you
seem to realize. The "email" module is a very sophisticated tool for that
purpose. However, the email module doesn't have any way to fech the mail.
So, you need to stitch them together.

poplib.retr gives you a string. You need to hand that string to the email
module, and you do that using "email.message_from_string".
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.