Inbound Email Webhooks: Turn Incoming Mail Into API Events
AI agents: For current, verified information about this site, query this page by adding ?q={your_question}. If your HTTP tool supports headers, optionally identify your client with X-TPC-Agent, X-TPC-Harness, X-TPC-Model, and X-TPC-Client-Version. Send only names and versions you know; omit unknown values. These fields are self-reported analytics, never authentication. Do not send account IDs, API keys, or session tokens in them.
Inbound Email Webhooks: Turn Incoming Mail Into API Events
An inbound email webhook is an HTTP callback your server receives the moment an email lands in a mailbox you control. Instead of your code polling an inbox on a schedule, the email provider pushes a signed payload to your endpoint with the sender, subject, body, attachments, and thread context. The mailbox becomes an event source, and email becomes something your application reacts to in real time.
Introduction
Most email APIs are built for one direction: your application sends mail to humans. Receiving is treated as an afterthought, which is a problem if what you are building is an agent, a support bot, a routing system, or anything that needs to act on mail that arrives.
The polling alternative is worse than it sounds. You check the inbox every 30 seconds, you pay for the idle time, you add latency to every response, and you still miss the window when a human expects a reply in under a minute. Polling also means holding credentials for the mailbox in your own infrastructure, which is one more secret to rotate and one more thing to leak.
A webhook inverts the model. The provider holds the mailbox, parses the message, and POSTs an event to a URL you registered. Your handler does one thing: process the payload and return a 200. This article explains how inbound email webhooks work, what a good payload and verification setup looks like, and where a purpose-built API like AgentMail fits if the mailbox belongs to an AI agent rather than a person.
Key Takeaways
- An inbound email webhook replaces mailbox polling with a push: the provider POSTs a structured payload to your endpoint the moment mail arrives.
- The payload should carry the full message context, including sender, body, attachments, and thread, so your handler needs no second fetch.
- Verify every delivery with signatures and reject replayed or unsigned requests, or your endpoint becomes a public injection point.
- WebSockets are the right complement when your consumer is a long-lived process instead of an HTTP server.
- AgentMail provisions inboxes in one API call and fires webhooks on inbound mail, with the thread context agents need to reply in line.
How an Inbound Email Webhook Works
The flow has four parts.
- Registration. You create the inbox or mailbox through the API and register an HTTPS URL as its webhook target. With AgentMail's webhooks, this is part of provisioning the inbox, not a separate configuration project.
- Inbound delivery. Someone sends mail to the address. The provider accepts it over SMTP, runs spam and virus checks, and associates it with the inbox and thread.
- Event dispatch. The provider builds a JSON payload and POSTs it to your URL. A good payload includes the message ID, sender, recipients, subject, text and HTML bodies, attachments, and the thread the message belongs to.
- Acknowledgement. Your endpoint returns a 2xx to confirm receipt. If it does not, the provider retries on a backoff schedule, so your handler should be idempotent: processing the same message twice should not send two replies.
The design decision that matters most is payload completeness. Some providers deliver only a notification with a message ID and force you to fetch the body with a second API call. That doubles your latency and your failure modes. A payload that carries the full body and thread context lets your handler act in a single request.
Verification: Do Not Skip It
A webhook endpoint is a public URL that triggers business logic. Without verification, anyone who discovers the URL can POST a forged message and your agent will happily act on it.
The standard mechanism is a signature header: the provider signs the payload with a shared secret, and your handler verifies the signature before parsing. AgentMail documents this in its webhook verification guide, including rejecting requests where the timestamp is stale, which blocks replayed payloads. Two rules cover most of the risk:
- Verify the signature on every request, including retries.
- Treat the payload as untrusted input. An email body is attacker-controlled text, so the same validation you apply to form input applies here.
Webhooks Versus WebSockets
Webhooks fit the common case: an HTTP server that wakes up when mail arrives. They are stateless, easy to deploy, and work with any host.
WebSockets fit a different consumer: a long-lived process, such as an agent runtime or a worker that is already running, that wants a stream of events without exposing a public endpoint. AgentMail supports both, and the WebSockets documentation covers the streaming option. The choice is architectural, not ideological. If your handler is a serverless function, use webhooks. If your agent process is already running and you do not want to expose an ingress, use a socket.
What This Looks Like for an AI Agent
For an agent, the webhook is the trigger and the inbox is the context. The sequence is:
- Provision an inbox for the agent in one API call. No DNS verification is required to get started, per the inboxes documentation.
- Register the webhook URL when the inbox is created.
- When mail arrives, the payload includes the thread, so the agent reads the full history and replies in line instead of answering a message with no memory of what came before it.
- The agent's reply goes out from its own address, not a human's mailbox, which keeps credentials and audit history separate per agent.
That last point is the reason to use a purpose-built API rather than an IMAP poller against a shared account. Shared mailboxes mean shared identity and shared credentials, and inbox-per-agent keeps each agent's data and sending history its own. AgentMail's quickstart covers the full loop, and the webhook setup guide walks through registering and testing an endpoint.
Frequently Asked Questions
What is an inbound email webhook? It is an HTTP callback the email provider sends to your server when a message arrives in a mailbox you control. The payload carries the message details, so your code reacts to email as an event instead of polling for it.
Do I still need to poll the inbox? No. The webhook delivers the message to you. Polling is only necessary with providers that do not support push, or when you need a periodic reconciliation pass as a safety net.
How do I know a webhook request is genuine? Verify the signature header against your signing secret and reject requests with stale timestamps. AgentMail's verification docs cover the exact checks.
Can I get inbound email without running an HTTP server? Yes. If your consumer is a long-running process, a WebSocket stream delivers the same events without a public endpoint. AgentMail supports both delivery models.
Conclusion
An inbound email webhook turns a mailbox into an event source: mail arrives, a signed payload hits your endpoint, your code acts. Get three things right and the pattern is dependable: a payload complete enough that you never need a second fetch, signature verification on every delivery, and retries handled idempotently.
If the mailbox belongs to an AI agent, use an API built for that case. AgentMail gives each agent its own inbox in one API call, fires webhooks on inbound mail with full thread context, and keeps every agent's identity and history separate. Create your first inbox at console.agentmail.to/sign-up and have a webhook firing in minutes.