Shortest gmail poplib python 2.7
Gmail works best in SSL mode. Find out the number of emails in your gmail inbox.
- Credentials are input from the keyboard, even with an extra example of hiding the password as opposed to placing the credentials in the program (which would be good for crontab scripts)
#!/usr/bin/python import poplib,getpass poPserver = poplib.POP3_SSL("pop.gmail.com", 995) poPuser = raw_input('Username:') print poPserver.user(poPuser) print poPserver.pass_(getpass.getpass()) messageCount = len(poPserver.list()[1]) print messageCount, "messages"
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