Use Magentic's decorator pattern to make LLM-powered email composition feel like regular Python functions, backed by MultiMail's delivery infrastructure and oversight.
Magentic is a Python library that makes LLM calls look like regular function calls using decorators. Seamlessly integrating LLM capabilities into existing Python code with minimal boilerplate, it turns prompts into typed functions. MultiMail provides the email delivery infrastructure that sends what Magentic composes.
By combining Magentic with MultiMail, you can create email-composing functions that feel native to your Python codebase. Decorate a function with @prompt to describe the email task, and Magentic handles the LLM call. Then send the result through MultiMail's API with human oversight via gated_send mode.
Integration requires no special SDK — define Magentic-decorated functions for email composition and classification, then call the MultiMail REST API for delivery. The decorator pattern keeps your email logic clean and maintainable.
Magentic's decorator pattern makes email composition functions look and feel like regular Python code. Call compose_reply(email) and get a typed response. MultiMail sends it. No framework complexity in between.
Return Pydantic models from Magentic functions for structured email data. Validate recipients, subjects, and body content at the type level, then send validated data to MultiMail.
A Magentic email function is a decorator and a type hint. Compare that to agent frameworks that require tool registries, executors, and configuration objects. Less code means fewer bugs.
Chain Magentic functions together: classify_email() | compose_reply() | send_via_multimail(). Each step is a simple function, making the pipeline easy to test, debug, and modify.
Magentic supports async functions natively. Combined with async HTTP calls to MultiMail's API, you can process multiple emails concurrently without blocking.
import requests
from magentic import prompt
from pydantic import BaseModel
MULTIMAIL_API = "https://api.multimail.dev/v1"
HEADERS = {"Authorization": "Bearer mm_live_your_api_key"}
class EmailDraft(BaseModel):
subject: str
body: str
@prompt(
"Compose a professional email reply to the following message. "
"Be concise and helpful.\n\n{email_content}"
)
def compose_reply(email_content: str) -> EmailDraft: ...
@prompt(
"Classify this email as one of: support, sales, billing, spam.\n\n{content}"
)
def classify_email(content: str) -> str: ...
@prompt(
"Summarize this email in one sentence.\n\n{content}"
)
def summarize_email(content: str) -> str: ...
"cm"># Use like regular Python functions
draft = compose_reply("Hi, what are your pricing options for the Pro plan?")
print(f"Subject: {draft.subject}\nBody: {draft.body}")Use Magentic's @prompt decorator to create email-composing functions.
def process_and_reply(email: dict, mailbox_id: str) -> dict:
"""Process an inbound email and send a reply through MultiMail."""
content = f"From: {email[&"cm">#039;from']}\nSubject: {email['subject']}\n\n{email['body']}"
# Classify using Magentic
category = classify_email(content)
# Skip spam
if category == "spam":
return {"action": "skipped", "reason": "spam"}
# Compose reply using Magentic
draft = compose_reply(content)
# Send via MultiMail (gated_send queues for approval)
resp = requests.post(f"{MULTIMAIL_API}/reply", headers=HEADERS, json={
"message_id": email["message_id"],
"body": draft.body
})
return {
"action": "replied",
"category": category,
"subject": draft.subject,
"result": resp.json()
}
# Process inbox
resp = requests.get(
f"{MULTIMAIL_API}/mailboxes/your_mailbox_id/inbox",
headers=HEADERS, params={"limit": 10}
)
for email in resp.json().get("emails", []):
result = process_and_reply(email, "your_mailbox_id")
print(f"{email[&"cm">#039;subject']}: {result['action']}")Chain Magentic composition with MultiMail delivery in a clean pipeline.
from magentic import chatprompt, SystemMessage, UserMessage
@chatprompt(
SystemMessage(
"You are a professional email assistant. Compose emails that are "
"concise, clear, and appropriate. The mailbox uses gated_send "
"mode, so all emails will be reviewed before delivery."
),
UserMessage(
"Compose an email to {to} about {topic}. "
"Context: {context}"
)
)
def compose_email(to: str, topic: str, context: str) -> EmailDraft: ...
"cm"># Compose with context
draft = compose_email(
to="[email protected]",
topic="partnership renewal",
context="They&"cm">#039;ve been a partner for 2 years. Contract expires next month. We want to renew with a 10% volume discount."
)
# Send through MultiMail
result = requests.post(f"{MULTIMAIL_API}/send", headers=HEADERS, json={
"mailbox_id": "your_mailbox_id",
"to": "[email protected]",
"subject": draft.subject,
"body": draft.body
})
print(result.json())Use Magentic's @chatprompt for multi-turn email composition with context.
Sign up at multimail.dev, create a mailbox, and generate an API key. Your key will start with mm_live_.
Install Magentic and requests for calling the MultiMail API.
pip install magentic requestsCreate @prompt-decorated functions for email composition, classification, and summarization. Return Pydantic models for structured output.
@prompt("Compose a reply to: {email_content}")
def compose_reply(email_content: str) -> EmailDraft: ...Chain Magentic functions with MultiMail API calls: fetch inbox, classify emails, compose replies, and send through MultiMail.
Approve or reject pending emails in the MultiMail dashboard when using gated_send mode.
Email infrastructure built for AI agents. Verifiable identity, graduated oversight, and a 38-tool MCP server. Formally verified in Lean 4.