# Send & Receive Emails with LangChain (/integrations/frameworks/langchain)

<!-- agent-signals: reading_time_min: 4 · est_tokens: 1754 · updated: 2026-09-06 -->
Related: [Send & Receive Emails with Claude Cowork](/integrations/frameworks/claude-cowork.md), [Send & Receive Emails with Claude Code](/integrations/frameworks/claude-code.md), [Send & Receive Emails with OpenAI Codex](/integrations/frameworks/codex.md), [Send & Receive Emails with OpenClaw](/integrations/frameworks/openclaw.md), [Send & Receive Emails with Grok](/integrations/frameworks/grok.md), [Send & Receive Emails with Manus](/integrations/frameworks/manus.md)



# Give a LangChain or LangGraph agent an email inbox

The `langchain-agentmail` package turns an AgentMail inbox into standard `langchain-core` tools, a document loader, a retriever, and typed webhook events. Use it when an agent built with `create_agent` or any LangGraph graph needs to send, search, and reply to email from its own `@agentmail.to` address.

## Do this

Python 3.10 or newer. Install from GitHub, export `AGENTMAIL_API_KEY` and `OPENAI_API_KEY`, then build the agent. Any LangChain chat model works in place of `ChatOpenAI`.

```bash
pip install "git+https://github.com/agentmail-to/langchain-agentmail.git" langchain langchain-openai numpy
export AGENTMAIL_API_KEY="<API_KEY>"
export OPENAI_API_KEY="<OPENAI_API_KEY>"
```

```python
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI

from langchain_agentmail import AgentMailToolkit

toolkit = AgentMailToolkit.from_api_key()  # reads AGENTMAIL_API_KEY
agent = create_agent(
    ChatOpenAI(model="gpt-4o-mini"),
    tools=toolkit.get_tools(),
    system_prompt="You are an assistant with your own AgentMail email inbox.",
)

result = agent.invoke({"messages": [(
    "user",
    "Create an inbox, then send an email from it to you@example.com "
    "with the subject 'Hello from my agent' and a one-line introduction.",
)]})
print(result["messages"][-1].content)
```

## SDK

Install: `pip install "git+https://github.com/agentmail-to/langchain-agentmail.git"`. For webhook verification, install the `webhooks` extra plus a server: `pip install "langchain-agentmail[webhooks] @ git+https://github.com/agentmail-to/langchain-agentmail.git" uvicorn`.

* Build the toolkit: `AgentMailToolkit.from_api_key()` reads `AGENTMAIL_API_KEY`.
* Get all 14 tools: `toolkit.get_tools()`.
* Use one tool without the toolkit: `AgentMailSendTool(client=AgentMailClient()).invoke({"inbox_id": "...", "to": "...", "subject": "...", "text": "..."})`.
* Load messages as documents: `AgentMailLoader(inbox_id="<inbox_id>", limit=100).load()`.
* Keyword search without embeddings: `AgentMailRetriever(inbox_id="<inbox_id>", k=5).invoke("invoice")`.
* Verify a delivery: `langchain_agentmail.webhooks.verify_signature(payload=raw_body, secret=..., svix_id=..., svix_timestamp=..., svix_signature=...)`.
* Parse a verified payload: `langchain_agentmail.webhooks.parse_event(payload)`.
* Register the webhook with the core SDK: `AgentMail().webhooks.create(url="https://<host>/agentmail", event_types=["message.received"])` returns the signing secret.

Core SDK and CLI installs: /integrations/sdks-and-cli

## Facts

* Toolkit tools, one per AgentMail operation: `agentmail_list_inboxes`, `agentmail_create_inbox`, `agentmail_list_threads`, `agentmail_get_thread`, `agentmail_list_messages`, `agentmail_get_message`, `agentmail_send_message`, `agentmail_reply_to_message`, `agentmail_update_message_labels`, `agentmail_create_draft`, `agentmail_update_draft`, `agentmail_send_draft`, `agentmail_delete_draft`, `agentmail_get_attachment`.
* Tools return results as JSON strings with HTML stripped and long bodies truncated.
* `agentmail_create_draft` supports scheduled delivery via `send_at`. `agentmail_get_attachment` returns an expiring download URL. `agentmail_reply_to_message` supports reply-all.
* `AgentMailLoader` yields one `Document` per message. `page_content` is the plain-text body with quoted history stripped. `metadata` carries `inbox_id`, `message_id`, `thread_id`, `from`, `to`, `subject`, `labels`, `timestamp`, and attachment details.
* `AgentMailLoader` fetches the full body of every message it loads. Set `limit` on large inboxes.
* Sent messages carry AgentMail's plain-text signature inside the body text.
* `parse_event` returns typed models for `message.received`, `message.sent`, `message.delivered`, `message.bounced`, `message.complained`, `message.rejected`, and `domain.verified`.
* The webhook signing secret starts with `whsec_` and is returned by `webhooks.create`. Store it as `AGENTMAIL_WEBHOOK_SECRET`.
* Webhook URLs must be reachable from the public internet over HTTPS.
* A delivery passes `verify_signature` only when signed with the webhook's secret over the exact raw request body.
* Creating an inbox with a taken username returns a `403` listing up to 3 available alternatives in `suggestions`. The full error text goes to the model, so the agent can pick one and retry in the same run.
* A reply-only agent needs three tools: `agentmail_list_threads`, `agentmail_get_thread`, `agentmail_reply_to_message`. Filter `toolkit.get_tools()` by `tool.name` before passing the list to `create_agent`.

## Not supported

* The documented install source is the GitHub URL above, not a plain `pip install langchain-agentmail`.
* Tool failures do not raise exceptions. Errors return to the model as text, so an agent can report success after a failed operation. Inspect the `ToolMessage` entries in `result["messages"]` when an outcome looks wrong.
* `verify_signature` does not accept a JSON body that was decoded and re-encoded. Verify the exact raw bytes first, parse after.

## Errors

| Error                                     | Status                 | Cause                                                                                 | Fix                                                                                            |
| ----------------------------------------- | ---------------------- | ------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| `ValueError: AgentMail API key not found` | none, Python exception | `AGENTMAIL_API_KEY` unset when the toolkit or a tool is built without an explicit key | Export `AGENTMAIL_API_KEY` before building the agent                                           |
| `InvalidSignatureError`                   | return `400`           | Wrong `AGENTMAIL_WEBHOOK_SECRET`, or the body was re-serialized before verification   | Verify the raw request body with the secret from `webhooks.create`                             |
| `403` from `agentmail_create_inbox`       | 403                    | Requested inbox username is taken                                                     | Use one of the up to 3 names in `suggestions`, or omit the username so AgentMail generates one |

## Verify

```bash
python -c "
from langchain_agentmail import AgentMailToolkit
for tool in AgentMailToolkit.from_api_key().get_tools():
    print(tool.name)
"
```

Success prints the 14 `agentmail_*` tool names. `ValueError: AgentMail API key not found` means `AGENTMAIL_API_KEY` is unset.

## Related

* /advanced/webhooks - hosting, delivery retries, and deduplication for inbound events
* /core/send - attachments, HTML bodies, and the operations behind the send tools
* /integrations/mcp-and-skills - hosted MCP server as an alternative to this package
* /advanced/safety - boundaries for agents steered by untrusted email
