A Beginner’s Guide to Deleting E-mails Using Python

Addison Chen
codeburst
Published in
4 min readMar 2, 2021

--

There will be times when you want to delete hundreds of spam emails from your inbox daily or delete certain alert emails that come in at fifteen-minute intervals. In that case, should you spend that time manually deleting all those emails yourself, or would it be easier to set up an automation script using Python? As someone who likes to have everything automated, I would rather learn and build that script to run daily to delete all my random alerts or retail subscription spams.

I will be going over the fundamental workflow of how emails are being deleted using Python. To do this, you will need to know terminologies such as the Internet Message Access Protocol, also known as IMAP — this is a protocol to communicate with the email provider’s server to retrieve the emails sent to your email address. Another type of protocol that retrieves emails is the Post Office Protocol Version 3, also known as POP3 — this is a protocol used to retrieve emails from the server. The use of POP3 can also delete that email after being downloaded from the server. Below I listed the prerequisite that you should take a look at below you continue to read on.

Prerequisites to look into:

Simple Workflow For Using IMAP In Python:

Imaplib Or Imapclient Or Exchangelib Or Poplib:

There are many IMAP packages in Python that you can use and look through the documentation to see which one works best for you. Imaplib package is a built-in package that comes with Python, so you do not have to install it. However, Imapclient and Exchangelib will require an install through the terminal before importing to your script. Despite already having an Imap package built-in, some people use the imapclient package because it is easier to use function-wise — additional documentation talking about the feature it comes with. Exchangelib package is specifically for those users that use the exchange email provider over the other options. The last option that I wanted to discuss is the poplib package which also can be used to read your messages, but it's best used for only one device — it is a simple protocol if you are mainly using it to delete emails.

How To Connect To An IMAP Server Using SSL Encryption:

I will be primarily using the Imaplib package for the examples below. However, I will give a quick sample of how you will use SSL to connect to the IMAP server using the Imapclient package.

Let's start by checking to see if your Python program has the imapclient or imaplib package. If not, then you will need to install it through the terminal.

pip install imapclient

Once installed, you can now import the package and use it for your scripts. There will be steps you need to take similar to my previous article on sending emails using SMTP.

Setting Up The Encryption Protocol In Python:

This is how I will declare the IMAP object if I imported the imaplib package. When using SSL, make sure you connect to the correct port number based on the email provider. In this case, Gmail is using port 992 to connect using the SSL encryption to the IMAP server.

import imaplibimapobj = imaplib.IMAP4_SSL('imap.gmail.com',993 )

Note: You can also connect using TLS Encryption — see the documentation for it here(IMAP4.starttls(ssl_context=None)).

This is how I will declare the IMAP object if I imported the IMAPclient package in Python.

from imapclient import IMAPClientimapobj = IMAPClient('imap.mailserver.com', use_uid=True, ssl=True)

Note: You can also connect using TLS Encryption — see the documentation for it here(starttls(ssl_context=None)).

IMAPlib — Declare the variable containing your account credentials:

import imaplib#Enter your username for your email provider
username = "your_email@gmail.com"
#Enter your password
password = input("Enter_Your_Password_Here")

Login to the IMAP Server using your account information:

imapobj.login(username, password)

Once You Log In, You Can Now Use The Package Functions To Delete Emails:

Select the mailbox, in this case, would be the spam box.

imapobj.login(username, password)
imapobj.select('Spam')

You can search the specific emails based on the FROM or SUBJECT.

result, message = imapobj.search(None, 'FROM "test@invalid.com"')result, message = imapobj.search(None, 'SUBJECT "This Is a SPAM Subject"')

If you never really look at your spam folder and need to make space, you can even delete all the Spam box emails at once.

result, message = imapobj.search(None, "ALL")

When you are finished, then make sure to close the mailbox using the function below.

imapobj.close()

Then finally, log out from your email account.

imapobj.logout()

Conclusion

Ideally, this article has shown you the potential for how IMAP functions can automate your emails. This is also a great way to learn a little bit about the protocols that email providers use to communicate with the servers. I hope this article was a great start to jumpstart your first script in automated emails!

--

--