Skip to main content

Python SDK for AgentMail — open-source email server for AI agents

Project description

AgentMail Python SDK

Python client for AgentMail — open-source email server for AI agents.

PyPI License Python

Install

pip install agentmail-oss

Quick Start

from agentmail import AgentMail

client = AgentMail(api_key="your-key", base_url="http://localhost:3456")

# Create an inbox
inbox = client.inboxes.create(display_name="My Agent")
print(inbox.email)  # agent-xxx@inbox.agentmail.dev

# Send an email
email = client.emails.send(
    inbox.id,
    to="user@example.com",
    subject="Hello from my agent",
    body="This email was sent by an AI agent.",
)

# List emails
emails = client.emails.list(inbox.id)
for e in emails.emails:
    print(f"{e.direction}: {e.subject}")

# Thread memory (the key differentiator)
memory = client.threads.get_memory(inbox.id, email.thread_id)
print(memory.summary)

# Semantic search
results = client.threads.search_memory(
    inbox.id, email.thread_id, query="revenue report"
)

Async

from agentmail import AsyncAgentMail

async with AsyncAgentMail(api_key="your-key") as client:
    inbox = await client.inboxes.create(display_name="Async Agent")
    email = await client.emails.send(inbox.id, to="user@example.com", body="Hello!")

Attachments

# Upload
attachment = client.attachments.upload(
    file=open("report.pdf", "rb"),
    filename="report.pdf",
)

# Send with attachment
client.emails.send(
    inbox.id,
    to="user@example.com",
    subject="Report attached",
    body="See attached.",
    attachment_ids=[attachment.id],
)

# Download
data = client.attachments.download(attachment.id)
with open("downloaded.pdf", "wb") as f:
    f.write(data)

Authentication

# Option 1: Pass explicitly
client = AgentMail(api_key="your-key")

# Option 2: Environment variable
# export AGENTMAIL_API_KEY=your-key
client = AgentMail()

# Custom server URL
# export AGENTMAIL_BASE_URL=http://your-server:3456
client = AgentMail(base_url="http://your-server:3456")

API Reference

client.inboxes

Method Description
.create(display_name?, metadata?) Create inbox
.list() List all inboxes
.get(inbox_id) Get inbox with stats
.delete(inbox_id) Delete inbox

client.emails

Method Description
.send(inbox_id, to=, subject?, body?, html_body?, attachment_ids?) Send email
.list(inbox_id, direction?, limit?, offset?) List emails
.get(inbox_id, email_id) Get email
.update(inbox_id, email_id, is_read?, is_archived?) Update email
.delete(inbox_id, email_id) Delete email

client.threads

Method Description
.list(inbox_id) List threads
.get(inbox_id, thread_id) Get thread with messages
.reply(inbox_id, thread_id, to=, body?) Reply to thread
.update(inbox_id, thread_id, is_archived?, labels?) Update thread
.delete(inbox_id, thread_id) Delete thread
.get_memory(inbox_id, thread_id) Get thread memory for agent prompts
.search_memory(inbox_id, thread_id, query=) Semantic search within thread
.refresh_memory(inbox_id, thread_id) Refresh rolling summary

client.attachments

Method Description
.upload(file=, filename?, content_type?) Upload attachment (25MB max)
.download(attachment_id) Download as bytes
.get_metadata(attachment_id) Get metadata
.delete(attachment_id) Delete attachment

client.webhooks

Method Description
.create(url=, events?, secret?) Register webhook
.list() List webhooks
.delete(webhook_id) Delete webhook

Error Handling

from agentmail import AgentMail, NotFoundError, AuthenticationError

try:
    client.inboxes.get("nonexistent-id")
except NotFoundError:
    print("Inbox not found")
except AuthenticationError:
    print("Invalid API key")

Requirements

License

MIT

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

agentmail_oss-0.1.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

agentmail_oss-0.1.0-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: agentmail_oss-0.1.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for agentmail_oss-0.1.0.tar.gz
Algorithm Hash digest
SHA256 494066171bdbd63fc8fdbb352f5a4f183fffd31fc7eb31807a08553cdcbd836e
MD5 64ac673443b9bbb697e2125146de21a3
BLAKE2b-256 213fba8029f79170553d94dcdc012084509fee8c9a09c8e086ee7531340e563f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: agentmail_oss-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for agentmail_oss-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eadaaf6b090273b8a7705c0de1ce161edc42a59926ca44483b379a2cff258989
MD5 8221efa0bfb973d7a53ae218717a23e4
BLAKE2b-256 9c12365b7ec197dbec06f291fc4219c255298f98b3f0e3395682f6c3ce1e4566

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