Receiving email using the poplib library

POP3 stands for Post Office Protocol version 3. This standard protocol helps you receive emails from a remote server to our local machine. The main advantage of POP3 is that it allows us to download emails on to our local machine and read the downloaded emails offline.

The POP3 protocol works on two ports:

  • Port 110: The default non-encrypted port
  • Port 995: The encrypted port

Now, we'll see some examples. First, we'll see an example where we get a number of emails. For that, create a script, number_of_emails.py, and write the following content in it:

import poplib
import getpass

pop3_server = 'pop.gmail.com'
username = 'Emaild_address'
password = getpass.getpass()

email_obj = poplib.POP3_SSL(pop3_server)
print(email_obj.getwelcome())
email_obj.user(username)
email_obj.pass_(password)
email_stat = email_obj.stat()
print("New arrived e-Mails are : %s (%s bytes)" % email_stat)

Run the script, as follows:

student@ubuntu:~$ python3 number_of_emails.py

As output, you'll get however many emails are present in your mailbox.

In the preceding example, first we're importing the poplib library, which is used in Python for the POP3 protocol to receive an email securely. Then, we state the specific email server and our email credentials—that is, our username and password. After that, we print the response message from the server and provide the username and password to the POP3 SSL server. After login, we get mailbox stats and print them to the Terminal in the form of a number of emails.

Now, we're going to write a script to get the latest email. For that, create a script, latest_email.py, and write the following content in it:

import poplib
import getpass

pop3_server = 'pop.gmail.com'
username = 'Emaild_address'
password = getpass.getpass()

email_obj = poplib.POP3_SSL(pop3_server)
print(email_obj.getwelcome())
email_obj.user(username)
email_obj.pass_(password)

print(" Latest Mail ")
latest_email = email_obj.retr(1)
print(latest_email[1])

Run the script, as follows:

student@ubuntu:~$ python3 latest_email.py

As output, you'll get the latest mail you received in your mailbox.

In the preceding example, we imported the poplib library used in Python to supply the POP3 protocol to receive an email securely. After stating the specific email server and the username and password, we printined the response message from the server and providing the username and password to the POP3 SSL server. Then, we're fetching the latest email from the mailbox.

Now, we're going to write a script to get all of the emails. For that, create a script, all_emails.py, and write the following content in it:

import poplib
import getpass

pop3_server = 'pop.gmail.com'
username = 'Emaild_address'
password = getpass.getpass()

email_obj = poplib.POP3_SSL(pop3_server)
print(email_obj.getwelcome())
email_obj.user(username)
email_obj.pass_(password)

email_stat = email_obj.stat()
NumofMsgs = email_stat[0]
for i in range(NumofMsgs):
for mail in email_obj.retr(i+1)[1]:
print(mail)

Run the script, as follows:

student@ubuntu:~$ python3 latest_email.py

As output, you'll get all of the emails you've received in your mailbox.

..................Content has been hidden....................

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