using poplib to retrieve messages sent from a certain address

R

Rybread

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 from someone
on this NG):

import poplib
import time

print 'Start at', time.asctime()

host = 'X'
port = 110
user = 'username'
pasw = 'password'

logfilename = 'sent_from_mobile_phone'
minsize = 130000
maxsize = 180000
fromtag = 'From user@domain %s\n'

ps = poplib.POP3(host, port)
ps.user(user)
ps.pass_(pasw)

messages = ps.list()
print '%d messages, %d bytes' % (len(messages[1]), messages[-1])

logfile = open(logfilename, 'a')

for sms in messages[1]:
sid, ssize = sms.split()
if minsize <= int(ssize) < maxsize:
message = ps.retr(sid)
print 'retrieving and deleting msg#%s, %d bytes, %d lines' % (
sid, message[-1], len(message[1]))
logfile.write(fromtag % time.asctime())
for line in message[1]:
logfile.write(line)
logfile.write("\n")
logfile.write('\n')
ps.dele(sid)

ps.quit()

print 'Done at', time.asctime()
print
 
A

Alex Martelli

Rybread said:
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 from someone
on this NG):

I was the "someone", and to retrieve the headers (and the first little
bit of a message) you could use 'top' method of Pop3 objects. However,
the docs warn you:

"""
top(which, howmuch)
Retrieves the message header plus howmuch lines of the message after the
header of message number which. Result is in form (response, ['line', ...],
octets).

The POP3 TOP command this method uses, unlike the RETR command, doesn't set
the message's seen flag; unfortunately, TOP is poorly specified in the RFCs
and is frequently broken in off-brand servers. Test this method by hand
against the POP3 servers you will use before trusting it.
"""

So, you'd be using that at your own risk. If you KNOW it works well
with your server, then, basically:
for sms in messages[1]:
sid, ssize = sms.split()
if minsize <= int(ssize) < maxsize:

resp, lines, octets = ps.top(sid, 1)

if "From: (e-mail address removed)" in lines:
message = ps.retr(sid)
print 'retrieving and deleting msg#%s, %d bytes, %d lines' % (
sid, message[-1], len(message[1]))
logfile.write(fromtag % time.asctime())
for line in message[1]:
logfile.write(line)
logfile.write("\n")
logfile.write('\n')
ps.dele(sid)

ps.quit()

print 'Done at', time.asctime()
print

or thereabouts (untested). You'll probably want to comment-out the
call to ps.dele until you're very secure that it all works...;-).

BTW, for generic tests of POP I find Garshol's popserve.py quite
useful, see http://www.garshol.priv.no/download/software/python/popserve.py
.. I think that, as coded, it doesn't support the TOP command (which
you need above), but it does mention a "more full-featured descendant"
that, for all I know, might.


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top