How to Retrieve Emails Using IMAP in Python

How to Retrieve Emails Using IMAP in Python

Published at - Nov 25, 2021

Good day there, In a recent piece, I discussed How to use Python SMTP to send emails?. It’s now time to start receiving emails via the IMAP protocol. But, before we begin, we must first understand IMAP, so let’s have a look. Python Program to Send Mail Using Gmail SMTP (with smtplib) In this lesson on Python coding, we will learn how to send mail using Gmail

What is IMAP?

IMAP stands for Internet Message Access Protocol, and it works similarly to SMTP in that it operates at the application layer via TCP/IP. Unencrypted connections utilize port 143, whereas encrypted connections use port 993.

Getting emails

POP Post Office Protocol is another option for this duty, but IMAP is preferable since it allows for greater synchronization between the client and the server, as well as access to more than just the email inbox.

Receiving emails is more difficult than sending them because you must additionally look for and understand the message:

Import packages

import email
import python-imap

We have to import these packages to use in our program.

Define credentials

EMAIL = '[email protected]'
PASSWORD = 'password'

You have to define your Gmail email id and password(generate app password if you are using two-factor auth)

Create & use App Passwords

If you use 2-Step-Verification and get a “password incorrect” error when you sign in, you can try to use an App Password.

  1. Go to your Google Account.

  2. Select Security.

  3. Under “Signing in to Google,” select App Passwords. You may need to sign in. If you don’t have this option, it might be because:

  4. 2-Step Verification is not set up for your account.

  5. 2-Step Verification is only set up for security keys.

  6. Your account is through work, school, or other organizations.

  7. You turned on Advanced Protection.

  8. At the bottom, choose Select app and choose the app you using

  9. Select the device and choose the device you’re using

  10. Generate.

  11. Follow the instructions to enter the App Password. The App Password is the 16-character code in the yellow bar on your device.

  12. Tap Done.

Connect to the server and read inbox

mail = imaplib.IMAP4_SSL(SERVER)
mail.login(EMAIL, PASSWORD)'inbox')

Now we need to connect to the server by logging into the account with the email and password that we created before. When you pick the mailbox to read messages from, you may select whatever you want, such as spam, draught, or send, depending on your preferences.

Get emails

status, data =, 'ALL')
for block in data:
    mail_ids += block.split()

We must now search for and pick email Ids from which to get emails; I am using None to select all emails from the inbox.

Fetch mail for each mail id

for i in mail_ids:
    status, data = mail.fetch(i, '(RFC822)')

All emails for a certain email id are stored in the data variable.

Full code

<iframe src="" frameborder=0></iframe>


How to generate credentials and start receiving emails using IMAP protocol” was described in the last instruction on getting emails with Gmail. I’m hoping you’ll enjoy it.

Thank you for reading this tutorial. If you have any doubt feel free to ask or provide your response and don’t forget to follow me. Happy coding!

By becoming a Medium member, you can support me and your other favorite authors. Thanks! 👇 Join Medium with my referral link - Harendra Verma As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…

More content at Sign up for our free weekly newsletter here.

About author

Harendra Kanojiya

Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.

Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile