473,503 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using poplib to parse headers

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
4 4748
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2982
by: Simon Burton | last post by:
This was so easy :) Simon Burton. #!/usr/bin/env python import sys from time import sleep from poplib import *
1
3010
by: Rybread | last post by:
Real quick, I have account X and I want a python script that goes in and looks for emails sent from Y and then to save them. i'm trying to go off the swen killer I have listed below (which i took...
2
6682
by: brettk | last post by:
Hello All, Here's what I'm trying to do: I need to connect to a pop3 server, download all messages, and copy all of the attachments into a specific directory. The actual email message is...
2
2680
by: Steve Greenland | last post by:
For the poplib.POP3 object, docs say: list() Request message list, result is in the form (response, ). If which is set, it is the message to list. But (I've folded the long line): Python...
0
1214
by: Frank Churchill | last post by:
Has anyone used poplib and popfile together? I've tried everything I can think of to specify SRVR in poplib: "127.0.0.1:8081" "127.0.0.1,port=8081" "localhost:8081" "localhost,port=8081" ...
4
2182
by: SuperHik | last post by:
Hi! I want to connect to gmail but... It requires SSL so I worte: Traceback (most recent call last): File "<interactive input>", line 1, in ? File "C:\Python24\lib\poplib.py", line 359, in...
6
6127
by: =?Utf-8?B?U2FtZWVrc2hh?= | last post by:
Hi, I want to write a simple .net program to open a URL, fill in fields, and click on a button to submit it using .net 1.1 framework. Can someone help in suggesting the libraries I should use?...
2
2997
by: SteveC | last post by:
Hello, I am trying to use POP3_SSL class of the poplib module to read email from my gmail account. I can connect just fine using the example here http://www.python.org/doc/lib/pop3-example.html...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.