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 4 4734
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 *
|
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...
|
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...
|
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...
|
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"
...
|
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...
|
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?...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
| |