AgentMail Python Library

0.4.15 · active · verified Mon Apr 13

The AgentMail Python library provides convenient access to the AgentMail APIs, an API-first email platform designed for AI agents. It allows programmatic creation and management of email inboxes, sending/receiving emails, and handling email-based workflows with webhooks and real-time events. The library is currently at version 0.4.15 and receives frequent updates, indicating an active development cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the AgentMail client using an API key from environment variables, create a new inbox (or use an existing one), and send a basic email. It includes basic error handling and highlights the common use of `python-dotenv` for API key management.

import os
from agentmail import AgentMail
from dotenv import load_dotenv

load_dotenv() # Load environment variables from .env file

# Ensure AGENTMAIL_API_KEY is set in your .env file or environment variables
api_key = os.environ.get('AGENTMAIL_API_KEY', '')

if not api_key:
    raise ValueError("AGENTMAIL_API_KEY environment variable not set.")

client = AgentMail(api_key=api_key)

def create_and_send_email():
    try:
        # Create an inbox (optional, often you'd use an existing one)
        # For idempotency, you can pass a client_id:
        # inbox = client.inboxes.create(username="my-agent-inbox", client_id="unique-id-123")
        inbox = client.inboxes.create()
        print(f"Inbox created: {inbox.inbox_id}")

        # Send an email
        send_response = client.inboxes.messages.send(
            inbox_id=inbox.inbox_id,
            to="recipient@example.com", # Replace with a real recipient email
            subject="Hello from AgentMail!",
            text="This is a test email sent from your AgentMail agent."
        )
        print(f"Email sent: Message ID {send_response.message_id}")

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    create_and_send_email()

view raw JSON →