lib pop objectpassing in the email server machine' name as stringserver poplib pop (mailserverif this call doesn' raise an exceptionwe're connected (by socketto the pop server listening on pop port number at the machine where our email account lives the next thing we need to do before fetching messages is tell the server our username and passwordnotice that the password method is called pass_ without the trailing underscorepass would name reserved word and trigger syntax errorserver user(mailuserserver pass_(mailpasswdconnectlog in to mail server pass is reserved word to keep things simple and relatively securethis script always asks for the account password interactivelythe getpass module we met in the ftp section of this is used to input but not display password string typed by the user once we've told the server our username and passwordwe're free to fetch mailbox information with the stat method (number messagestotal bytes among all messagesand fetch the full text of particular message with the retr method (pass the message number--they start at the full text includes all headersfollowed by blank linefollowed by the mail' text and any attached parts the retr call sends back tuple that includes list of line strings representing the content of the mailmsgcountmsgbytes server stat(hdrmessageoctets server retr( + octets is byte count we close the email server connection by calling the pop object' quit methodserver quit(else locked till timeout notice that this call appears inside the finally clause of try statement that wraps the bulk of the script to minimize complications associated with changespop servers lock your email inbox between the time you first connect and the time you close your connection (or until an arbitrarysystem-defined timeout expiresbecause the pop quit method also unlocks the mailboxit' crucial that we do this before exitingwhether an exception is raised during email processing or not by wrapping the action in try/finally statementwe guarantee that the script calls quit on exit to unlock the mailbox to make it accessible to other processes ( delivery of incoming emailfetching messages here is the popmail script of example - in actiondisplaying two messages in my account' mailbox on machine pop secureserver net--the domain name of the mail server machine used by the isp hosting my learning-python com domain nameas configured in the module mailconfig to keep this output reasonably sizedi've omitted or truncated few irrelevant message header lines hereincluding most of the client-side scripting