惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

DEV Community

93 Agents. 2.6 Billion Tokens. One Working OS. And a Bill Under $1,000. Stop Getting 'It Depends' Answers About RAG Architecture Wrapping Hermes Agent with agent-stack: six tiny libs for the boring parts Templating got me to 33,620 pages. Indexing them was the hard part. EML Attachments Not Opening? Here’s How to Fix the Issue Easily in 2026 WordPress client onboarding: the exact process I use to start every maintenance contract right Models shouldn't have execution authority. Why we built a deterministic FSM runtime for AI agents. AI makes building faster, but semantic distribution is now the hard part How I Created My First Solana Token from Scratch (SPL Token Basics Explained) How I use WP-CLI to cut WordPress maintenance time from 6 hours to 20 minutes What Is Vibe Coding? And Does It Actually Work for Production Code? (I Tested 10 Tools) WordPress staging environments: the 15-minute setup that prevents client emergencies Reading Log #2 — Sapiens Imagined Communities: An Organization Is Made of Shared Fiction I Built a Branded Token on Solana in 5 Minutes (No Smart Contract Needed) The Confidence Gap: How AI Introduces Silent Errors on Production Sites Day 7 - Dense Embedding - RAG Why teachers need explainable AI, not just accurate AI — building the KC dashboard Closing the feedback loop: how mistake classification drives adaptive problem selection in NumPath Amazon Quick: AWS's Agentic Workspace, Explained for Engineers "My Coding Agent Remembered Sessions, Not Work. That Was the Bug" Reading Log #0 — Manga Was a Democratization Device for Cultural Capital SecOps Salary Guide 2025: GRC CTC Ranges in Indian GCCs vs Product Companies 🐍 python global vs nonlocal keyword — when to use each? 6 Free Online Video Cutters That Don’t Completely Ruin Your Footage (2026) Engineering Around Bitcoin's Traditional Platform Lockdowns AI 2026 ⚖️ Case File 4.1: The Efficiency Extortion Client-Side AI: The Next Era of Consumer E-Commerce? Why I'm building an AI math tutor for dyscalculia — and grounding it in 30 years of ITS research "My DingTalk Coding Bot Said It Started the Task. Then It Never Sent the Result" Your trycatch sucks - lets fix it I Built a Globally Distributed Blog Platform for ~$1/Month Awesome-Claude-Skills I built 135 Claude Skills with real formulas. Here's what "production-grade" actually means. How I engineered a Non-Euclidean AI framework for massive data reduction Automated 25 Minutes of My Morning With a Prompt (Not a Script) أدوات API ذاتية الاستضافة: هل يجب أن تترك السحابة؟ Never Use Service Classes in Rails How Markus Builds AI Teams That Actually Ship — Not Just Chat Pricing logic feels boring until it's wrong. Software Engineer Skills Companies Want in 2026: 48K-Posting Analysis Data Races Reproduced: Harnesses That Catch Heisenbugs Demystifying AI Agents: Building an Agentic Pipeline From Scratch in Pure Python Coding Agents Are Becoming Remote Workers. Enterprises Need an Agent Harness. How I Let an AI Refactor My Whole Codebase (Using Gemini 3.5) Flutter 3.44 Highlights From Google I/O 2026: What's New and What Matters The Hidden Cleanup Cost Behind AI Coding Velocity Promises A beginner's guide to the Image-Background-Remove model by Zf-Kbot on Replicate A beginner's guide to the Invsr model by Zf-Kbot on Replicate How to Automate Canadian T4 Slip Parsing with an API (No OCR Setup Required) حماية مفاتيح API من إضافة VS Code ضارة Agetor Review: An Open-Source Kanban Board for Orchestrating Claude Code Why most Marketo audits start at the wrong layer RevOps alignment is an operating-model problem, not a tooling problem Why Some Developers Are Moving Away From Tailwind CSS in 2026 API 키 보호: 악성 VS 코드 확장으로부터 안전하게 VS Code拡張機能によるAPIキー漏洩を防ぐ方法 Temporal Cloud Serverless: Durable Execution Without the Ops Overhead Why Freshers Must Build Real AI Products Instead of Endless App Clones I Built a Dynamic llms.txt for Next.js. Then Google Said Don't Bother. AWS Summit Seoul 2026: Korean Enterprises And Agentic AI Does AI Know How Many Tokens It Is Burning Selling Software in Countries PayPal Can't Reach - A Cautionary Tale of Crypto and Custom Solutions My Old MacBook Air Couldn't Handle It — So I Used Google Colab to Train an AI#1 The Discord.js gotchas that cost me a week each (so they don't have to cost you one) Leetcode QOTD:- 3043. Find the Length of the Longest Common Prefix MPT DEX Performance Test Report I shipped a working landing page in 14 KB. Here is every byte. Zero-Secret CI/CD: GitHub Actions + OIDC on AWS (Part 6) Building the React Frontend: Document Library and Chat UI (Part 5) Runtime Governance Evidence Anchors in 2026: A Public Ledger for Budget and Accountability Decisions RAG and Vector Search with pgvector and Amazon Bedrock (Part 4) Serverless Document Pipelines with AWS Step Functions (Part 3) Multi-Tenant Auth with Cognito and PostgreSQL Row-Level Security (Part 2) Building a Multi-Tenant AI Document Platform on AWS (Part 1: Architecture) Building a Nutrition Calculator in JavaScript: filter, map, and reduce on Objects Shipping an MCP server: parallel search, JSON output, and what broke along the way Runtime Governance Evidence Anchors in 2026: A Public Ledger for Budget and Accountability Decisions A 3-step agent cost me $4.20. agenttrace showed me the O(n ) tool call hiding in plain sight. Beyond WebView: The Next Evolution of Hybrid App Architecture Our retry loop made an outage worse. The circuit breaker stopped the cascade. Claude returned ```json blocks 14% of the time. Here is the Rust crate I wish I had earlier. I burned my Anthropic org cap and waited 3 days. Then I built llmfleet. One Open Source Project a Day (No. 71): CodeGraph — Pre-Index Your Codebase for AI Agents, Save 35% Cost and 70% Tool Calls The prompt your SDK sends is not the prompt you wrote The Context Tax: Why Every Cursor Session Costs You 15 Minutes Prompt Physics: Building a Cognitive Steering Layer for Gemma 4 Pain Points Will Always Outlive Platforms 92. BERT: The Model That Reads in Both Directions QAOA vs. 75,000 Nodes: Building a Hybrid Architecture to Solve NP-Hard Problems When Quantum Simulators Hit a Wall E2B? E4B? 26B A4B? The Gemma 4 Model Names Finally Explained One Tool That Cuts Token Costs 40-80% for Claude Code, Codex, opencode, and openclaw Building a 32-URL economy microsite on top of a 754,000-row SQLite dataset Coordinating 100+ AI Agents in the Field: Practical Patterns for Robotic Swarms Static site search for Astro in 2026: why I picked Pagefind over Algolia and Lunr How I built pairwise AI model compare pages with Claude Haiku and a budget cap Three post-deploy checks I run after every Cloudflare Pages build Why I'm betting on AI-curated directories when Google AI Overviews answer the same queries When boto3 doesn't have it (yet), you write it: a realtime speech-to-speech story in Python Zero-Trust RAG: Defeating the Shared Private Link Deadlock in Azure Terraform You Can't Co-Design What You Don't Operate
A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design
Fernando Cel · 2026-04-18 · via DEV Community

The Problem

If you've ever tried to read or send emails with Python, you know the pain:

# The old way
import imaplib
import email

mail = imaplib.IMAP4_SSL("imap.gmail.com")
mail.login("user@gmail.com", "password")
mail.select("inbox")
_, data = mail.search(None, "UNSEEN")
for num in data[0].split():
    _, msg_data = mail.fetch(num, "(RFC822)")
    msg = email.message_from_bytes(msg_data[0][1])
    # now manually decode headers, parse body, handle attachments...

Low-level, verbose, and error-prone. You spend more time fighting the protocol than solving your actual problem.

And this is just for reading. If you want to send an HTML email with attachments, you're deep into MIMEMultipart, MIMEText, MIMEBase, encoders.encode_base64... pages of boilerplate before you even get to your actual logic.

Python's standard library gives you the building blocks, but it never gives you the house.

Why I Built This

I was working on a project that needed to automate email workflows — read incoming messages, extract attachments, reply programmatically, and back everything up. After writing the same IMAP boilerplate for the third time across different projects, I decided to build something better.

The goals were simple:

  • Zero configuration for common providers — just pass an email address and password
  • A Django ORM-like query API — composable, readable, Pythonic
  • Lazy evaluation — don't fetch 10,000 emails when you only need a count
  • Pydantic models everywhere — type-safe, serializable, predictable
  • One library for everything — read, send, search, reply, forward, backup

The result is email-profile.

Meet email-profile

pip install email-profile

from email_profile import Email

with Email("user@gmail.com", "app-password") as app:
    for msg in app.unread().messages():
        print(f"{msg.from_}: {msg.subject}")

That's it. 3 lines to read your unread emails. No server configuration, no manual connection handling, no protocol-level code.

Key Features

Auto-Discovery for 35+ Providers

No more googling "what's the IMAP server for Outlook?". Just pass your email address:

# Gmail, Outlook, Yahoo, iCloud, ProtonMail, Zoho...
# Server settings are auto-discovered
app = Email("user@outlook.com", "password")

It uses a 4-tier resolution strategy:

  1. Hard-coded provider map — Gmail, Outlook, Yahoo, iCloud, and 30+ others are mapped directly
  2. RFC 6186 SRV DNS lookups — standard service discovery for domains that support it
  3. MX record inference — extracts the provider from MX records and maps to known IMAP hosts
  4. Convention fallback — tries imap.<domain> and smtp.<domain> as a last resort

This means even custom domains hosted on known providers (like a company email on Google Workspace) are resolved automatically.

Powerful Search with Q & Query

If you've used Django's ORM, this will feel familiar. The Q class provides static methods that return composable expressions:

from email_profile import Q
from datetime import date

# Combine with & (AND), | (OR), ~ (NOT)
q = Q.from_("boss@company.com") & Q.subject("urgent") & Q.since(date(2025, 1, 1))

for msg in app.inbox.where(q).messages():
    print(msg.subject)

Available Q methods include Q.from_(), Q.subject(), Q.unseen(), Q.seen(), Q.since(), Q.before(), Q.larger(), and more. They can be combined freely with &, |, and ~ operators.

For simpler cases, use kwargs-style queries with Query:

from email_profile import Query

results = app.inbox.where(Query(subject="invoice", unseen=True))
print(f"Found {results.count()} unread invoices")

Query also supports chaining with .exclude() and .or_():

q = Query(subject="report").exclude(from_who="spam@example.com").or_(subject="invoice")
messages = app.inbox.where(q).list()

Send Emails (Plain Text, HTML, Attachments)

Sending is just as clean. Plain text, HTML, attachments, CC, BCC — all in one call:

app.send(
    to="team@company.com",
    subject="Weekly Report",
    html="<h1>Report</h1><p>All systems operational.</p>",
    attachments=["report.pdf", "metrics.csv"]
)

You can also send both plain text and HTML as a multipart message:

app.send(
    to="recipient@example.com",
    subject="Update",
    body="Plain text fallback for clients that don't render HTML.",
    html="<h1>Update</h1><p>Rich version of the same content.</p>"
)

Note that html is a string parameter (the HTML content itself), not a boolean flag. This is a deliberate design choice — it keeps the API explicit and avoids ambiguity.

Reply & Forward

Reply and forward are methods on the Email object, not on the message. This keeps Message as a pure data object (Pydantic model) with no side effects:

# Reply to sender only
app.reply(msg, body="Thanks, I'll review it today!")

# Reply all
app.reply(msg, body="Acknowledged.", reply_all=True)

# Forward
app.forward(msg, to="colleague@company.com", body="FYI - please take a look")

Mailbox Management

Access standard folders via properties, or reach any custom folder by path:

# Built-in shortcuts
app.inbox
app.sent
app.spam
app.trash
app.drafts
app.archive

# Custom folders
reports = app.mailbox("Projects/Reports")

Flag operations live on the mailbox, not the message. They accept a Message, a UID string, or an integer:

app.inbox.mark_seen(msg)
app.inbox.mark_unseen(msg)
app.inbox.flag(msg)
app.inbox.unflag(msg)
app.inbox.move(msg, "Archive")
app.inbox.copy(msg, "Backup")
app.inbox.delete(msg)

This design means you can operate on messages from any context — even messages loaded from a backup — without needing an active connection reference on the message object itself.

Sync & Backup to SQLite

Back up your entire mailbox to a local SQLite database with incremental sync:

from email_profile import StorageSQLite

app = Email("user@gmail.com", "password")
app.storage = StorageSQLite("backup.db")

with app:
    result = app.sync()  # incremental — only downloads new emails
    print(f"{result.inserted} new, {result.skipped} skipped")

The SyncResult object gives you full visibility: inserted, updated, deleted, skipped, and errors. Restore is just as simple:

with app:
    count = app.restore()  # returns int (count of restored messages)
    print(f"Restored {count} messages")

Lazy Evaluation

Queries return a Where object — nothing hits the server until you explicitly ask for data. This is critical for performance when working with large mailboxes:

query = app.inbox.where(Q.unseen())

# No IMAP call yet — the query is just a description
query.count()       # hits the server, returns int
query.exists()      # bool — cheaper than count for "any unread?" checks
query.first()       # Message or None — fetches only one
query.last()        # Message or None — fetches only one
query.list()        # list[Message] — materializes everything
query.messages()    # iterator — memory-efficient for large result sets

This means app.inbox.where(Q.unseen()).count() only asks the server "how many?", without downloading a single message body.

Fetch Modes

Control how much data you pull per message. This makes a huge difference when you're scanning thousands of emails:

# Full message + attachments (default)
app.inbox.where().messages(mode="full")

# Headers + body, skip attachments (faster)
app.inbox.where().messages(mode="text")

# Headers only (cheapest — great for building an index)
app.inbox.where().messages(mode="headers")

For example, if you're building a dashboard that shows subject lines and dates, use mode="headers" and save 90%+ of bandwidth.

Message as a Pydantic Model

Every message is a Message object — a Pydantic BaseModel with typed fields:

from email_profile import Message

for msg in app.inbox.where(unseen=True).messages():
    msg.subject          # str
    msg.from_            # str
    msg.to_              # str
    msg.date             # datetime
    msg.uid              # str
    msg.message_id       # str
    msg.body_text_plain  # Optional[str]
    msg.body_text_html   # Optional[str]
    msg.cc               # Optional[str]
    msg.bcc              # Optional[str]
    msg.reply_to         # Optional[str]
    msg.in_reply_to      # Optional[str]
    msg.references        # Optional[str]
    msg.content_type     # str
    msg.attachments      # list[Attachment]
    msg.headers          # dict
    msg.list_id          # Optional[str]
    msg.list_unsubscribe # Optional[str]

Because it's Pydantic, you get .model_dump(), .model_dump_json(), validation, and serialization for free. This makes it trivial to pipe email data into APIs, databases, or AI workflows.

Real-World Recipes

Monitor inbox for new messages

with Email.from_env() as app:
    for msg in app.unread().messages():
        print(f"From: {msg.from_} | Subject: {msg.subject}")
        app.inbox.mark_seen(msg)

Download PDF attachments from invoices

with Email("user@gmail.com", "password") as app:
    for msg in app.search("invoice").messages():
        for att in msg.attachments:
            if att.content_type == "application/pdf":
                att.save("./invoices/")

Backup all emails to SQLite

from email_profile import StorageSQLite

app = Email("user@gmail.com", "password")
app.storage = StorageSQLite("full_backup.db")

with app:
    result = app.sync()
    print(f"Backed up {result.inserted} new emails")

Clean up spam folder

with Email.from_env() as app:
    for msg in app.spam.where().messages():
        app.spam.delete(msg)

Search across date ranges

from datetime import date
from email_profile import Q

with Email.from_env() as app:
    q = Q.since(date(2025, 1, 1)) & Q.before(date(2025, 4, 1)) & Q.from_("client@company.com")
    messages = app.inbox.where(q).list()
    print(f"Found {len(messages)} emails from client in Q1 2025")

Use environment variables

# .env
EMAIL_SERVER=imap.gmail.com
EMAIL_USERNAME=user@gmail.com
EMAIL_PASSWORD=app-password

with Email.from_env() as app:
    print(f"Connected as {app.user}")

You can also customize the variable names:

app = Email.from_env(
    server_var="MY_IMAP_HOST",
    user_var="MY_EMAIL",
    password_var="MY_EMAIL_PW"
)

Built-in Error Handling & Retry

The library provides specific exception classes so you can handle failures gracefully:

from email_profile import ConnectionFailure, NotConnected, QuotaExceeded, RateLimited

try:
    app.connect()
except ConnectionFailure:
    print("Could not connect to server")
except QuotaExceeded:
    print("Mailbox quota exceeded")
except RateLimited:
    print("Too many requests — back off and retry")

For automated workflows, use the built-in retry decorator:

from email_profile.retry import with_retry

@with_retry(attempts=3, initial_delay=1.0, max_delay=30.0)
def fetch_unread():
    with Email.from_env() as app:
        return app.unread().list()

It handles exponential backoff automatically, so your scripts survive transient network issues without custom retry logic.

Why email-profile?

Auto-discovery No 35+ providers Context manager No Yes Search API Raw IMAP commands Q objects & Query kwargs Send HTML Manual MIME building html="<h1>..." Attachments Manual encoding attachments=["file.pdf"] Reply/Forward Build from scratch app.reply(msg, body="...") Backup/Sync DIY StorageSQLite built-in Lazy evaluation No Where objects Retry logic DIY @with_retry decorator Pydantic models No Message DTO Fetch modes Manual flags mode="full/text/headers"
Feature imaplib/smtplib email-profile

Design Principles

A few decisions that shape the library:

  • Message is data, not behavior. Message is a pure Pydantic model. It has no methods that mutate state or require a connection. Operations like mark_seen(), move(), and delete() live on the mailbox. This keeps the model serializable and testable.

  • Lazy by default. .where() returns a Where object, not a list. You choose when and how to materialize: .count(), .first(), .list(), or .messages(). This prevents accidentally downloading thousands of emails.

  • One import, one object. For most use cases, from email_profile import Email is all you need. The Email class is the entry point for connecting, reading, sending, replying, forwarding, and syncing.

  • Fail explicitly. Instead of generic exceptions, the library provides ConnectionFailure, NotConnected, QuotaExceeded, and RateLimited — so you can handle each case differently.

Getting Started

pip install email-profile

from email_profile import Email

with Email("your@email.com", "app-password") as app:
    # Read
    for msg in app.unread().messages():
        print(msg.subject)

    # Send
    app.send(to="friend@email.com", subject="Hello!", body="Sent with email-profile")

Links

The library is in active development and we'd love your feedback. Star the repo, open issues, or contribute — every bit helps!

What do you think? Would you use this in your projects? Drop a comment below!