shortest poplib example

shortest poplib example python 2.7

short python 2.7 poplib tutorial, python27 poplib library usage lesson, poplib example

Get it at GitHub: https://github.com/weblanta/pytbl-spe

#!/usr/bin/python

from poplib import *

poPserver = POP3('aaa.bbb.ccc.ddd') # ip works better than url
print poPserver.getwelcome() # todo: convert print to debug
print poPserver.user('email@address.com') # todo: chng print to dbg
print poPserver.pass_('EmailPassword') # todo: s/print/debug.validate(
messagesInfo = poPserver.list()[1]
numMessages = len(messagesInfo)

for i in range(numMessages):
  messageIs = poPserver.retr(i+1)[1]
  print "==========================",
  print " message number",i,
  print "=========================="
  for j in messageIs:
    print j
    continue

A longer example puts the login credentials up front and easier to get to here;

#!/usr/bin/python

from poplib import *

popIp='aaa.bbb.ccc.ddd'
popAddr='email@address.com'
popPass='EmailPassword'

popSrvr = POP3(popIp) # ip works better than url
print popSrvr.getwelcome() # todo: convert print to debug
print popSrvr.user(popAddr) # todo: chng print to dbg
print popSrvr.pass_(popPass) # todo: s/print/debug.validate(
messagesInfo = popSrvr.list()[1]
numMessages = len(messagesInfo)

for i in range(numMessages):
  messageIs = popSrvr.retr(i+1)[1]
  print "==========================",
  print " message number",i,
  print "=========================="
  for j in messageIs:
    print j
    continue

and now in this example we split it into a class;

#!/usr/bin/python

from poplib import *

popIp='aaa.bbb.ccc.ddd'
popAddr='email@address.com'
popPass='EmailPassword'

class poppins:

  def __init__(self):
    self.popSrvr = POP3(popIp) # ip works better than url
    print self.popSrvr.getwelcome() # todo: convert print to debug
    print self.popSrvr.user(popAddr) # todo: chng print to dbg
    print self.popSrvr.pass_(popPass) # todo: s/print/debug.validate(
    messagesInfo = self.popSrvr.list()[1]
    self.numMessages = len(messagesInfo)

popJones=poppins()
for i in range(popJones.numMessages):
  messageIs = popJones.popSrvr.retr(i+1)[1]
  print "==========================",
  print " message number",i,
  print "=========================="
  for j in messageIs:
    print j
    continue

finally, we move the credentials and the boiler-plate into a library

# class in the poppins thingamajig
from poplib import *

class poppinsMary:
    def __init__(self):
        popIp='aaa.bbb.ccc.ddd'
        popAddr='email@address.com'
        popPass='EmailPassword'

class poppins:

  def __init__(self):
    x=poppinsMary()
    self.popSrvr = POP3(x.popIp) # ip works better than url
    print self.popSrvr.getwelcome() # todo: convert print to debug
    print self.popSrvr.user(x.popAddr) # todo: chng print to dbg
    print self.popSrvr.pass_(x.popPass) # todo: s/print/debug.validate(
    messagesInfo = self.popSrvr.list()[1]
    self.numMessages = len(messagesInfo)

Which allows us a really short email reading python program here

#!/usr/bin/python

import popCredentials

popJones=popCredentials.poppins()
for i in range(popJones.numMessages):
  messageIs = popJones.popSrvr.retr(i+1)[1]
  print "==========================",
  print " message number",i,
  print "=========================="
  for j in messageIs:
    print j
    continue
 

0 thoughts on “shortest poplib example”

  1. Greetings from Colorado! I am bored at work so I decided to check out your site on my iphone during lunch break. I love the information you provide here and ca not wait to take a look when I get home. I am amazed at how quick your blog loaded on my cell phone .. I am not even using WIFI, just 3G .. Anyhow, excellent blog!

Leave a Reply

Your email address will not be published. Required fields are marked *