LangChain Email Integration for AI Agents | OpenMail
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
01
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.
Node.js
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"
02
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.
Node.js
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 again
03
Pass 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.
env
OPENMAIL_API_KEY=om_... # scoped to this inbox only
OPENMAIL_INBOX_ID=inb_8f3a1b2c
OPENMAIL_ADDRESS=agent@yourdomain.com
04
Send and reply from the agent
Your LangChain agent sends email via the API. Save the threadId to reply in the same thread later.
Python
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()
05
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.
Python
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.
Frequently asked questions
How do I give a LangChain agent an email address?
One API call to POST /v1/inboxes creates a live inbox on your domain and returns the address. Pass OPENMAIL_API_KEY, OPENMAIL_INBOX_ID, and OPENMAIL_ADDRESS as environment variables to your agent. Full guide at docs.openmail.sh/quickstart/api.
Does OpenMail require a LangChain-specific SDK?
No. OpenMail is a REST API — any LangChain agent that can make HTTP requests works with it. No additional SDK or package required beyond your HTTP client of choice.
How does my LangChain agent receive inbound emails?
Connect to wss://api.openmail.sh/v1/ws with your API key. OpenMail delivers inbound emails as structured JSON events in under 500ms — no polling, no public webhook URL needed.
Can a LangChain agent handle a full email conversation autonomously?
Yes. OpenMail delivers inbound emails with full thread history as structured JSON. Your agent receives the message in context, generates a reply, and sends it using the threadId to keep the conversation threaded correctly.
Is there a free tier?
Yes. Three inboxes and 3,000 emails per month. No credit card required.