Skip to main content

A lightweight IMAP-based email reader for Python/Django

Project description

mailpeek

A lightweight Python library for reading unread emails via IMAP.

Installation

poetry add mailpeek

Basic Usage

from mailpeek.reader import EmailReader

reader = EmailReader(
    host="imap.gmail.com",
    email="your-email@gmail.com",
    password="your-app-password"
)

emails = reader.fetch_unread()
for mail in emails:
    print(mail["subject"], mail["from"])

Fetch All Emails with Limit

emails = reader.fetch_emails(unread_only=False, limit=10)

Filter Attachments

Only PDFs:

emails = reader.fetch_unread(attachment_filename_contains=".pdf")

Only images:

emails = reader.fetch_unread(attachment_mime_startswith="image/")

Fetch Attachments On-Demand

for mail in emails:
    for att in mail["attachments"]:
        stream = reader.get_attachment_stream(mail["uid"], att["part_id"])
        with open(att["filename"], "wb") as f:
            f.write(stream.read())

Use with IMAP IDLE (Real-Time Mail Listener)

from mailpeek.imap_idle_listener import IMAPIdleListener

def on_new_mail(msg):
    print("\n📥 New email:", msg.get_subject())

def on_disconnect(error):
    print(f"🔌 Disconnected: {error}")

listener = IMAPIdleListener(
    host="imap.gmail.com",
    email="your-email@gmail.com",
    password="your-app-password",
    callback=on_new_mail,
    on_disconnect=on_disconnect,
)

listener.start()

To stop listening:

listener.stop()

Django Integration

  • Create a management/commands/read_emails.py command that calls fetch_unread()
  • Use get_attachment_stream() to save files into FileField
  • Run via cron or Celery

CLI Usage

Install with:

poetry add mailpeek

Run with:

poetry run mailpeek --email your-email@gmail.com --password your-app-password

Optional:

--all              # Fetch read + unread
--limit 20         # Only get 20 emails
--filename .pdf    # Only attachments with .pdf in name
--mime image/      # Only attachments starting with MIME image/

Or directly:

python src/mailpeek/cli.py --email your-email@gmail.com --password your-app-password

PyPI Packaging

Make sure your pyproject.toml includes:

[project]
name = "mailpeek"
version = "0.1.0"
description = "A lightweight IMAP-based email reader for Python/Django"
readme = "README.md"
license = "MIT"
authors = ["Anand R Nair <anand547@outlook.com>"]
dependencies = ["imapclient >=3.0.1", "pyzmail36 >=1.0.5"]
requires-python = ">=3.9"

[project.scripts]
mailpeek = "mailpeek.cli:main"

[tool.poetry]
packages = [{ include = "mailpeek", from = "src" }]

[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"

Then:

poetry build
poetry publish --username __token__ --password <pypi-token>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mailpeek-0.1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mailpeek-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file mailpeek-0.1.0.tar.gz.

File metadata

  • Download URL: mailpeek-0.1.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/24.5.0

File hashes

Hashes for mailpeek-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6eb9c677b04b3a9ee3f3cadc809785aaa6a3c219c0a81d095f1b0d34c62595e8
MD5 a35ff241628ff07bb590e79794839d6d
BLAKE2b-256 cf6cbc21d3a41b871a34e769de645c6ea9e2461c3c956ba0c8356297a9521bc6

See more details on using hashes here.

File details

Details for the file mailpeek-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mailpeek-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/24.5.0

File hashes

Hashes for mailpeek-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e5d81ac991d950ebca6e072cbef3fb5213fe72ac0ef24afbfa98bb35548aae1
MD5 da8b7431d0d256ce8cb6462db7c8243f
BLAKE2b-256 85e2115c8d68dd50c52a0dcb6ab6e1c454b51dc22d0ad1169d0e6006050e1aae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page