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.pycommand that callsfetch_unread() - Use
get_attachment_stream()to save files intoFileField - 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
Release history Release notifications | RSS feed
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)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6eb9c677b04b3a9ee3f3cadc809785aaa6a3c219c0a81d095f1b0d34c62595e8
|
|
| MD5 |
a35ff241628ff07bb590e79794839d6d
|
|
| BLAKE2b-256 |
cf6cbc21d3a41b871a34e769de645c6ea9e2461c3c956ba0c8356297a9521bc6
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e5d81ac991d950ebca6e072cbef3fb5213fe72ac0ef24afbfa98bb35548aae1
|
|
| MD5 |
da8b7431d0d256ce8cb6462db7c8243f
|
|
| BLAKE2b-256 |
85e2115c8d68dd50c52a0dcb6ab6e1c454b51dc22d0ad1169d0e6006050e1aae
|