[Tutor] poplib questions

fleet@teachout.org fleet@teachout.org
Thu, 20 Sep 2001 20:15:51 -0400 (EDT)


The pop3 mail retrieval module constructed from the example in poplib.py (see below) seems to
work REALLY well.  I have a couple of "newbie" type questions, though:

1. I need the mail to go to my /var/spool/mail/fleet file. (Linux RH 7.1)
Where should I open and close the file?  Open (for append) just before the
"for i in range" statement and close after "a.quit()" as I have it below?
Or should I open the file before the first getmail() call in the main
program and close it after the last one?  Does it make any difference?

2. Is there any reason for the three spaces before each line printed?

3. Do I have the "import" in the correct place or should it be in within
the function?

4. Are there any serious security concerns with this script (other than
the fact that the account names and passwords are in a text file)?

5. I've inserted some linefeeds, else the messages become one huge long
line in the "testbox"  file.  Is this going to mess up my procmail filter?

6. And, totally off topic, since there are no instructions activating
procmail in my fetchmail script, I'm assuming that procmail "guards" the
/var/spool/mail/fleet file and activates if anything shows up.  Am I
close?

7. Any suggestions to improve this would be greatly appreciated.

				- fleet -

++++++++++++++++++++++++++++++++++++++++++++
#! /usr/bin/python

import getpass, poplib

def getmail(MAILSERVER, USER, PASS):
   """Retrieves mail from pop3 servers"""

   a = poplib.POP3(MAILSERVER)
#   print a.getwelcome()
   a.user(USER)
   a.pass_(PASS)
   a.list()
   (numMsgs, totalSize) = a.stat()
   if numMsgs == 0:
      print "\nNo mail for "+USER+" at "+MAILSERVER
      return
   else:
      print "Retrieving "+`numMsgs`+" messages for "+USER+" at "+MAILSERVER
      f=open("testbox","a")
   for i in range(1, numMsgs + 1):
           (header, msg, octets) = a.retr(i)
           print "Message ", `i`, "("+`octets`+" octets)", ':'
           for line in msg:
                   f.write('   ' + line +"\n")
                   print ".",
#            dele(i)
#           f.write('\n-----------------------\n')
   a.quit()
   f.close()
   return

print "\n\n\n\n"
getmail("mail.server.com","fleet","secret")
getmail("mail.server.com","fleet1","secret")
getmail("mail.server.com","fleet2","secret")
getmail("mail.server.com","fleet3","secret")
getmail("mail.server.com","fleet4","secret")

++++++++++++++++++++++++++++++++++++++++++++++++++++++++