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. Map the inbox_id to your agent instance for routing.
const response = await fetch("https://api.openmail.sh/v1/inboxes", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENMAIL_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ mailboxName: userId }),
});
const inbox = await response.json();
// inbox.id → "inb_8f3a1b2c"
// inbox.address → "agent@yourdomain.com"Pass credentials to your agent
Three environment variables. The API key is shared across agents; inbox ID and address are unique per agent.
OPENMAIL_API_KEY=om_...
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.
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":
# Route to the right agent by inbox_id
inbox_id = event["inbox_id"]
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.