Email for LangChain Agents
Give every LangChain agent its own inbox. Create inboxes via API, deliver inbound emails as structured JSON, and handle full conversation threads — no SMTP config, no deliverability headaches.
How OpenMail works with LangChain
Create an inbox per agent
One API call provisions a live inbox on your domain and returns the address immediately. This runs in your provisioning code with your account key — the agent never sees that key.
const response = await fetch("https://api.openmail.sh/v1/inboxes", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENMAIL_ACCOUNT_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ mailboxName: userId }),
});
const inbox = await response.json();
// inbox.id → "inb_8f3a1b2c"
// inbox.address → "agent@yourdomain.com"Mint a key scoped to that inbox
Give each agent a key that reaches only its own inbox. It can read and send from that one inbox and nothing else — so a prompt injection in one agent's mail can't reach another agent's inbox. The token is returned once; store it with the agent's config.
const res = await fetch(
`https://api.openmail.sh/v1/inboxes/${inbox.id}/api-keys`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENMAIL_ACCOUNT_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ name: `agent:${userId}` }),
},
);
const { token } = await res.json();
// token → "om_..." shown once, never retrievable againPass credentials to your agent
Three environment variables. All three are unique per agent — the key is the inbox-scoped one you just minted, not your account key.
OPENMAIL_API_KEY=om_... # scoped to this inbox only
OPENMAIL_INBOX_ID=inb_8f3a1b2c
OPENMAIL_ADDRESS=agent@yourdomain.comSend and reply from the agent
Your LangChain agent sends email via the API. Save the threadId to reply in the same thread later.
import os, requests
def send_email(to, subject, body, thread_id=None):
payload = {"to": to, "subject": subject, "body": body}
if thread_id:
payload["threadId"] = thread_id
response = requests.post(
f"https://api.openmail.sh/v1/inboxes/{os.environ['OPENMAIL_INBOX_ID']}/send",
headers={"Authorization": f"Bearer {os.environ['OPENMAIL_API_KEY']}"},
json=payload,
)
return response.json()Receive inbound emails as structured events
Connect via WebSocket — no public URL needed. When an email arrives, OpenMail fires an event with the parsed message body, thread context, and any extracted attachments. An inbox-scoped key only ever streams its own inbox, so each agent sees just its own mail.
import asyncio, json, os, websockets
async def listen():
uri = "wss://api.openmail.sh/v1/ws"
headers = {"Authorization": f"Bearer {os.environ['OPENMAIL_API_KEY']}"}
async for ws in websockets.connect(uri, extra_headers=headers):
try:
await ws.send(json.dumps({"type": "subscribe"}))
async for raw in ws:
event = json.loads(raw)
if event.get("event") == "message.received":
# Scoped key → only this agent's inbox arrives here
message = event["message"]
# Pass to your LangChain agent
except websockets.ConnectionClosed:
continue
asyncio.run(listen())What your LangChain agent can do with email
Send emails autonomously from a branded address on your domain
Receive replies in real time via WebSocket — no polling
Read full thread history as structured JSON for context
Parse PDF, CSV, and DOCX attachments automatically
Handle OTP codes and verification links for autonomous signup flows
What's included
Dedicated inbox per agent
Each LangChain agent gets its own address on your domain — no shared inboxes, no filtering logic.
Real-time inbound delivery
WebSocket events delivered in under 500ms. Your agent receives email the moment it arrives.
Full thread history as structured JSON
Pull complete conversation context via the history endpoint and feed it directly into your agent's context window.
Attachment parsing
PDF, CSV, and DOCX files automatically extracted to plain text. Your agent gets the content without handling MIME types.
Managed deliverability
SPF, DKIM, and DMARC configured automatically on provisioning. Primary inbox from day one.
Works with any LangChain version
REST API, no SDK required. Any LangChain agent that can make HTTP requests works with OpenMail.