[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: selecting which messages to download based on headers



ice.dp wrote:

> On 5/8/05, s. keeling <keeling@spots.ab.ca> wrote:
>> Why?  What are you trying to do?
> I'm cheking my mail via GPRS. It's $1 per Mb in my country.
> It's very expencive for me. But i'm subscribed to this list,
> and I don't want to download every message on this list.
> I want download only messages with subjects that are interesting to me
> 

This may help you. We use it clean up mailboxes similar to what you're trying 
to do. It may at least give you some ideas. You could pull down all the 
headers and then delete anything you don't want to read. Then use your mail 
client to retrieve what's left on the server. 

It may be white space damaged by my mailer.

###############################################
#!/usr/bin/python
#
# This script is a helper to clean POP3 mailboxes
# containing malformed mail that hangs MUA's, that 
# are too large, or whatever...
#
# It iterates over the non-retrieved mails, prints
# selected elements from the headers and displays
# the message number. 
#
# Written by Xavier Defrang <xavier.defrang@brutele.be>
# Hacked by Eric Gaumer <egaumer@pagecache.org>
# 

import getpass, poplib, re

# Change this to your needs
POPHOST = ""
POPUSER = ""
POPPASS = ""

# How many lines of message body to retrieve
MAXLINES = 10

# Headers we're actually interrested in
rx_headers  = re.compile(r"^(From|To|Subject)")

try:

    # Connect to the POPer and identify user
    pop = poplib.POP3(POPHOST)
    pop.user(POPUSER)

    if not POPPASS:
        # If no password was supplied, ask for it
        POPPASS = getpass.getpass("Password for %s@%s:" % (POPUSER, POPHOST))

    # Authenticate user
    pop.pass_(POPPASS)

    # Get some general information (msg_count, box_size)
    stat = pop.stat()

    # Print some useless information
    print "\nLogged in as %s@%s using password %s" % (POPUSER, POPHOST, 
POPPASS)
    print "Status: %d message(s), %d bytes\n" % stat

    choice = raw_input("(d=delete message, s=show messages, q=quit) Option: ")
    delete = 0
    
    if choice in "sS":
    	for n in range(stat[0]):
        	msgnum = n+1
        	
		# Retrieve headers
        	response, lines, bytes = pop.top(msgnum, MAXLINES)

        	# Print message info and headers we're interrested in
        	print "Message %d (%d bytes)" % (msgnum, bytes)
        	print "-" * 30
        	print "\n".join(filter(rx_headers.match, lines))
        	print "-" * 30
    elif choice in "dD":
    	msgnum = raw_input("Message number to do delete: ")
	pop.dele(msgnum)
	delete = 1
    elif choice in "qQ":
    	pass
	

    # Summary
    if (delete):
    	print "\nDeleting 1 message in mailbox %s@%s" % (POPUSER, POPHOST)

    # Commit operations and disconnect from server
    print "Closing POP3 session\n"
    pop.quit()

except poplib.error_proto, detail:

    print "POP3 Protocol Error:", detail


-- 
Eric Gaumer
Debian GNU/Linux PPC
egaumer@pagecache.org
http://egaumer.pagecache.org
PGP/GPG Key 0xF15D41E9

Attachment: pgpbaeP0x6Lrw.pgp
Description: PGP signature


Reply to: