Simple Python library for reading verification emails via IMAP — OTP extraction, link parsing, auto IMAP detection.
Project description
imap_reader
Simple, clean Python library for reading verified emails via IMAP — with OTP extraction, verification link parsing, and auto IMAP detection.
Zero external dependencies — uses Python standard library only (imaplib, email, re, threading).
##Installation
pip install imap_reader
or
pip install git+https://github.com/nabilunnuha/imap_reader.git
Quick Start
from imap_reader import ImapClient
with ImapClient(
email_address="user@gmail.com",
password_email="your_app_password",
) as client:
for msg in client.get_messages("UNSEEN"):
print(msg.subject)
print(msg.otp_codes) # ['123456']
print(msg.verification_links) # ['https://example.com/verify?token=...']
Features
- IMAP auto-detect — automatically detects the IMAP host of an email domain
- OTP extraction — extracts a 5–6-digit code from the email body
- Verification link extraction — finds and prioritizes verification/confirmation links
- HTML email parser — converts HTML emails to plain text for extraction
- Login timeout — raises an error if the connection timeout is exceeded (30–60 seconds)
- Context manager — use the
withstatement to automatically close the connection - Delete messages — delete emails based on criteria (
ALL,SEEN,UNSEEN) - Type hints — fully typed, IDE-friendly, and autocomplete-friendly
Usage
Basic — Read unseen emails
from imap_reader import ImapClient
with ImapClient("user@gmail.com", "app_password") as client:
for msg in client.get_messages("UNSEEN"):
print(f"From : {msg.sender}")
print(f"Subject : {msg.subject}")
print(f"OTP : {msg.otp_codes}")
print(f"Links : {msg.verification_links}")
Retrieve only 1 recent email
with ImapClient("user@gmail.com", "app_password") as client:
msg = client.get_latest_message("UNSEEN")
if msg:
print(msg.otp_codes)
Custom timeout (30–60 seconds recommended)
with ImapClient(
email_address="user@gmail.com",
password_email="app_password",
timeout=60, # seconds
) as client:
msg = client.get_latest_message()
Read from specified folder
with ImapClient("user@gmail.com", "app_password") as client:
for msg in client.get_messages("ALL", folder="SPAM"):
print(msg.subject)
Delete email
with ImapClient("user@gmail.com", "app_password") as client:
# Delete all read emails
count = client.delete_messages("SEEN")
print(f"{count} deleted emails")
# Delete all emails in INBOX
count = client.delete_messages("ALL", folder="INBOX")
# Delete unread emails in SPAM
count = client.delete_messages("UNSEEN", folder="SPAM")
Add custom IMAP provider
from imap_reader import ImapClient
from imap_reader.models import DEFAULT_IMAP_LIST, ImapItemModel
DEFAULT_IMAP_LIST.append(
ImapItemModel(domain="mycompany.com", imap_host="imap.mycompany.com")
)
with ImapClient("user@mycompany.com", "password") as client:
for msg in client.get_messages():
print(msg)
Error handling
from imap_reader import ImapClient
from imap_reader.exceptions import (
AuthenticationError,
ConnectionTimeoutError,
ImapNotFoundError,
)
try:
with ImapClient("user@gmail.com", "wrong_password", use_default_imap=False, timeout=10) as client:
msg = client.get_latest_message()
except AuthenticationError:
print("Email or password is incorrect.")
except ConnectionTimeoutError:
print("Connection to IMAP server timed out.")
except ImapNotFoundError:
print("Email domain not recognized, add manually to imap_list.")
API Reference
ImapClient(email_address, password_email, imap_list=None, use_default_imap=True, timeout=30)
| Parameters | Type | Default | Description |
|---|---|---|---|
email_address |
str |
— | Complete email address |
password_email |
str |
— | IMAP Password or App Password |
imap_list |
list |
DEFAULT_IMAP_LIST |
List of IMAP providers |
use_default_imap |
bool |
True |
IMAP Password or App Password |
timeout |
int |
30 |
Login timeout in seconds |
Methods
| Method | Returns | Description |
|---|---|---|
get_messages(criteria, folder, limit, mark_seen) |
Generator[EmailMessage] |
Fetch and yield messages |
get_latest_message(criteria, folder) |
EmailMessage | None |
Fetch 1 most recent message |
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
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 imap_reader-1.0.2.tar.gz.
File metadata
- Download URL: imap_reader-1.0.2.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df53b7f43ba563531c865006cf19e86a318151e2e525b8d64cbc47313f7f1670
|
|
| MD5 |
708c49e270933fb6196fefdb01204541
|
|
| BLAKE2b-256 |
2aa0c99d0d60289415fcd64fef9bda643ef87454752ded4c422aea527f546b34
|
File details
Details for the file imap_reader-1.0.2-py3-none-any.whl.
File metadata
- Download URL: imap_reader-1.0.2-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c33d9f50ae6d73cfc97a4ee90d6ba41a766247634e442ccfc970e67608a61c9
|
|
| MD5 |
61f56bd26b3b09bd6af6d9041c0c0432
|
|
| BLAKE2b-256 |
668400db0793040681d76fc99a602dfc3e7b9cc0778457948f10cb4ed0924644
|