Overview
AgentMailChannel keeps a conversation as one email thread in an AgentMail inbox. Each post is an email from the inbox to the people in to: the first starts the thread and the rest reply in it. People on the thread can reply from their own mail client, and their replies join the conversation under their email address.
It talks to the AgentMail REST API over HTTPS, so there is no SMTP or IMAP server to configure. To use a mailbox you already run instead, see SmtpChannel.
Import
Setup
- Create an account and an inbox at agentmail.to. The inbox id is its address, such as
research@agentmail.to. - Create an API key and export it:
Constructor
str
required
The inbox the agents send from, such as
research@agentmail.to.str | List[str]
required
The address or addresses that get each post and can reply.
str
default:"Swarms conversation"
The subject of the first email.
str
default:"None"
An existing AgentMail thread to join instead of starting one. The first post then replies to the thread’s newest message.
str
default:"None"
An AgentMail API key. Defaults to the
AGENTMAIL_API_KEY environment variable.str
default:"https://api.agentmail.to"
The API host. Use
https://api.agentmail.eu for the EU region.float
default:"30.0"
Seconds to wait for each call.
ValueError if no API key is passed or set.
Attributes
How messages are sent and read
AgentMail adds its own footer and the quoted thread to each email body. So thatmessages() can read a post back exactly, each post carries two headers:
messages() reads every page of the thread and returns the messages oldest first:
The two headers are trusted only on mail the inbox sent. A reply that sets
X-Swarms-Agent itself is still attributed to its From address.
Methods
post()
Send one email. The first post (with nothread_id) sends a new message with subject. Later posts reply to the channel’s previous post, so the thread stays in order.
httpx.HTTPStatusError on a non-2xx reply.
messages()
Read the whole thread, oldest first. Returns[] before the first post when no thread_id was given.
httpx.HTTPStatusError on a non-2xx reply.
clear()
Delete the thread from the inbox, so the next post starts a new one. Emails already delivered stay in the recipients’ mailboxes.close()
Close the connection pool. The thread stays in the inbox.Usage Examples
A person approves before the swarm continues
Rejoin a thread later
Notes
- Every read is an API call per page. An agent reads the channel on every run.
- Treat replies as untrusted input. Anyone who can email the inbox and is on the thread can add to the conversation the agents read.