How to do it...

You need to offer a valid email address and password to send an email through this recipe. We pass the email server, POP3 port, user and password as the arguments, and receive the password to your email account using the getpass library (so that your email password is not displayed in plain text).

Listing 5.11 gives the simple POP3 email client as follows:

#!/usr/bin/env python 
# Python Network Programming Cookbook, Second Edition -- Chapter - 5 
# This program is optimized for Python 2.7.12 and Python 3.5.2. 
# It may run on any other version with/without modifications. 
 
import getpass  
import poplib 
import argparse 
 
def mail_client(host, port, user, password): 
    Mailbox = poplib.POP3_SSL(host, port)  
    Mailbox.user(user)  
    Mailbox.pass_(password)  
    numMessages = len(Mailbox.list()[1]) 
    print (Mailbox.retr(1)[1]) 
    Mailbox.quit() 
 
 
if __name__ == '__main__': 
    parser = argparse.ArgumentParser(description='Mail Server Example') 
    parser.add_argument('--host', action="store", 
dest="host", type=str, required=True) parser.add_argument('--port', action="store",
dest="port", type=int, required=True) parser.add_argument('--user', action="store",
dest="user", type=str, required=True) password = getpass.getpass("Enter your Password:") given_args = parser.parse_args() mail_client(given_args.host, given_args.port,
given_args.user, password)

This recipe logs in as a simple Python-based POP3 email client for your email account, and retrieves an email from your email account. Running this program gives the following output:

$ python 5_11_pop3_mail_client.py --host='pop.googlemail.com' --port=995 [email protected] 
Enter your Password:
[b'Received: by 10.70.31.12 with HTTP; Mon, 24 Dec 2007 11:48:08 -0800 (PST)', b'Message-ID: <[email protected]>', b'Date: Tue, 25 Dec 2007 01:18:08 +0530', b'From: "KANAPATHIPILLAI KATHIRAVELU
....
[Retrieved Email Truncated Here..]
  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.159.178