imaplib

The imaplib module provides tools for interacting with mail servers using the Internet Message Access Protocol (IMAP). This module allows you to connect to an IMAP server, authenticate, and perform operations such as fetching and deleting emails.

Here’s a quick toy example of how you’d use imaplib to log in to your email server:

Python
>>> import imaplib
>>> mail = imaplib.IMAP4_SSL("imap.example.com")
>>> mail.login("username", "password")
('OK', [b'Authenticated'])
>>> mail.logout()

Key Features

  • Connects to IMAP servers with or without SSL
  • Authenticates with a username and password
  • Lists mailboxes on the server
  • Fetches email messages and their parts
  • Searches mailboxes using various criteria
  • Deletes and moves email messages

Frequently Used Classes and Functions

Object Type Description
imaplib.IMAP4 Class Provides basic IMAP functionality
imaplib.IMAP4_SSL Class Extends IMAP4 to use SSL for secure connections
imaplib.IMAP4.login() Method Authenticates with the server
imaplib.IMAP4.select() Method Selects a mailbox for operations
imaplib.IMAP4.search() Method Searches the mailbox for specific messages
imaplib.IMAP4.fetch() Method Retrieves specific messages from the server
imaplib.IMAP4.logout() Method Logs out and closes the connection

Examples

Connecting to an IMAP server and logging in:

Python
>>> import imaplib
>>> mail = imaplib.IMAP4_SSL("imap.example.com")
>>> mail.login("username", "password")
('OK', [b'Authenticated'])

Selecting a mailbox and searching for all emails:

Python
>>> mail.select("inbox")
('OK', [b'42'])
>>> result, data = mail.search(None, "ALL")
>>> mail_ids = data[0].split()

Fetching an email by ID:

Python
>>> result, data = mail.fetch(mail_ids[0], "(RFC822)")
>>> raw_email = data[0][1]
>>> mail.logout()

Common Use Cases

  • Connecting to an IMAP server to access emails
  • Searching for emails with specific criteria
  • Fetching and reading email content
  • Deleting or moving emails within mailboxes

Real-World Example

Here’s how you can connect to an IMAP server, search for unread emails, and fetch their subject lines:

Python
import imaplib, email

mail = imaplib.IMAP4_SSL("imap.example.com")
mail.login("username", "password")
mail.select("inbox")

result, data = mail.search(None, "UNSEEN")
for num in data[0].split():
    result, msg_data = mail.fetch(num, "(RFC822)")
    msg = email.message_from_bytes(msg_data[0][1])
    print(msg["subject"])

mail.logout()

In this example, the imaplib module is used to connect to an email server, search for unread emails in the inbox, and print their subject lines.

Tutorial

Sending Emails With Python

In this tutorial, you'll learn how to send emails using Python. Find out how to send plain-text and HTML messages, add files as attachments, and send personalized emails to multiple people.

intermediate web-dev

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated July 10, 2025