5 Best Ways to Sort Important Emails from Different Mailboxes in Python

Rate this post

πŸ’‘ Problem Formulation: Email users often face the challenge of managing multiple mailboxes flooded with both essential and trivial messages. The task at hand is to programmatically identify and sort important emails using Python. The goal is to filter emails based on subject lines, sender, keywords, or other criteria from various mailboxes (such as Gmail, Outlook, etc.), with the output being a curated list of emails deemed important.

Method 1: Using Python’s Built-in imaplib and email Modules

Python’s standard imaplib module allows users to connect to their email server and retrieve messages. The email module can then parse these messages. By combining these two modules, users can create filters to sort through messages and identify important ones based on custom criteria like sender or subject.

Here’s an example:

import imaplib
import email

# Establish a connection to the email server
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('your_email@gmail.com', 'password')

# Select the inbox to search for emails

# Search for specific criteria (e.g., emails from a certain sender)
status, messages = mail.search(None, '(FROM "trusted_sender@example.com")')

# Fetch and parse the important emails
for num in messages[0].split():
    status, data = mail.fetch(num, '(RFC822)')
    message = email.message_from_bytes(data[0][1])



Important Meeting Today
Project Deadline Reminder

This script connects to a Gmail account, selects the inbox, and searches for emails from “trusted_sender@example.com”. It then fetches and prints the subject of these emails. Note that sensitive credentials should be managed securely, such as with environment variables.

Method 2: Using the mail-parser Library

The mail-parser library simplifies email parsing. It takes raw email data and provides a parsed object with accessible properties. Sorting important emails can be achieved by applying conditions to these properties.

Here’s an example:

from imaplib import IMAP4_SSL
import mailparser

# Connect to the email server
with IMAP4_SSL('imap.gmail.com') as mail:
    mail.login('your_email@gmail.com', 'password')

    status, messages = mail.search(None, 'ALL')

    for num in messages[0].split():
        status, data = mail.fetch(num, '(RFC822)')
        mail_obj = mailparser.parse_from_bytes(data[0][1])
        if "urgent" in mail_obj.subject.lower():



URGENT: Update Required

This code utilizes the mail-parser library to search through all emails in the inbox and prints out subjects containing the word “urgent”. It provides a user-friendly interface to easily access email attributes.

Method 3: Using Gmail API with googleapiclient

The Gmail API provides a flexible and powerful way to interact with your Gmail account programmatically. Using the googleapiclient library in Python, users can query their mailbox and sort emails with much more sophistication including utilizing Gmail’s powerful search syntax.

Here’s an example:

from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials

# Load credentials and build the service
creds = Credentials.from_authorized_user_file('token.json')
service = build('gmail', 'v1', credentials=creds)

# Use the Gmail API to search for emails with 'high importance' in the label
results = service.users().messages().list(userId='me', q='label:important').execute()
messages = results.get('messages', [])

for msg in messages:
    msg = service.users().messages().get(userId='me', id=msg['id']).execute()



Finances report: action required
Meeting rescheduled to 3 PM

This code retrieves and displays snippets from emails labeled as ‘important’ in a Gmail account. The Gmail API allows the use of Gmail’s search syntax, providing a potent way to define filters for sorting emails.

Method 4: Utilizing mailbox Module for Local Mailbox Files

The mailbox module in Python is excellent for parsing local mailbox files (like mbox, often used on Unix systems). It can iterate through messages and apply filters to identify important emails for users who back up their email locally or interact with server mailboxes via file transfer.

Here’s an example:

import mailbox

# Open the mailbox
mbox = mailbox.mbox('/path/to/mbox')

# Iterate through messages and find important ones
for message in mbox:
    if "Action Required" in message['subject']:



Action Required: Account Verification
Action Required: Confirm Subscription

This snippet opens a local mbox file and prints subjects of emails containing “Action Required”. This method is beneficial when working directly with mailbox files.

Bonus One-Liner Method 5: Using List Comprehensions with imaplib

For Python users who love one-liners, the imaplib module can also be utilized to fetch and sort emails using a compact list comprehension, ideal for quick scripts or programmers who prefer concise code.

Here’s an example:

import imaplib, email

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('your_email@gmail.com', 'password')
type, data = mail.search(None, 'ALL')
important_subjects = [email.message_from_bytes(mail.fetch(num, '(RFC822)')[1][0][1])['subject'] for num in data[0].split()]



['Project Deadline', 'Urgent: Meeting Reminder', ...]

This one-liner fetches all the emails in the inbox and creates a concise list of their subjects. It is elegant but more challenging to read and maintain.


  • Method 1: Using imaplib and email. Strengths: Part of the Python standard library, no external dependencies. Weaknesses: Can be verbose and requires manually parsing emails.
  • Method 2: Using mail-parser. Strengths: Easy to use and parse with. Weaknesses: External library that needs to be installed, less control over low-level operations.
  • Method 3: Using Gmail API with googleapiclient. Strengths: Powerful, utilizes Gmail’s advanced search, and fully featured. Weaknesses: Requires OAuth setup and Gmail only.
  • Method 4: Utilizing mailbox. Strengths: Good for working with local mailbox files. Weaknesses: Not suitable for interacting with server mailboxes directly.
  • Bonus One-Liner Method 5: Using list comprehensions with imaplib. Strengths: Concise. Weaknesses: Potentially less readable and less practical for complex filtering.