473,385 Members | 1,461 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

using poplib to retrieve messages sent from a certain address

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
Jul 18 '05 #1
1 3003
Rybread wrote:
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: fo*@bar.com" 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/...on/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

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Giobibo | last post by:
I use the following code to send e-mails with PHP: if(@mail(....)) { //Send OK } else { //Send Bad } But how can I verify that a message was really sent (in case of "Sent
0
by: Will Price | last post by:
I am having problems using UDPClient.Send() in System.Net.Sockets. Each time I make the call to the function, there is a significant delay from when the code is executed to when the packet...
3
by: Pebble | last post by:
Hi folks, I have a WebService that simply acts as a thin wrapper to a .Net dll. Literally almost zero code in the WebService. Parameters passed to the WebService are simply used as...
1
by: ManningFan | last post by:
Ha! Now THIS should truly be the final piece. I've recieved an email, I've sent a reply depending on the subject, but now I also want to prevent certain emails I've sent from being stored in...
4
by: kang jia | last post by:
hi i am doing mailinglist currently. the code in my first page is like this : : <html> <head> <link rel="stylesheet" type="text/css" href="gallery.css" /> <script language="JavaScript"> ...
0
by: Hollywood | last post by:
Good day dear members of the comp.databases.ms-access newsgroup. I'm using the new Acess 2007 "Collect data by using e-mail messages" functionality but it seems the generated e-mail by Outlook...
4
by: Jean-Claude Neveu | last post by:
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....
0
by: jase | last post by:
can anyone direct me how to set a system wide hook to access the print structure sent to the os when a print command is fired from any application???
2
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.