By Aljay Miskino, VP of Partnerships at Email for Agents
The most expensive bug in an agent’s email integration is not a 400. It is a successful send to a real person while you were “just testing.” If every inbox in your stack is a routable address, a CI job or a half-finished prompt will eventually do that.
This walkthrough shows a safer loop: use a test project whose addresses cannot route, inject inbound mail through an API, and send with an idempotency key so a retry is a replay, not a second message. The examples use the Email for Agents API, where I work. The habits transfer if you build the mailbox yourself.
Start in a test project, and keep it a test project
A project is test or live from the moment you create it, and that choice is fixed. There is no promotion step. Keep two projects and swap credentials.
Create an inbox in the test project. The address comes back on .mail.invalid, for example 3f7c…@a91d….mail.invalid. .invalid is reserved by RFC 6761 so that it can never resolve. That is stronger than a flag that suppresses sending: even if every application guard failed, DNS has no answer. The transport is simulated as well; a test send is never handed to the live provider.
You cannot pick a custom local part or attach a custom domain on a test project — that restriction is what makes it safe to point an unproven agent at the inbox. Asking for either returns test_mode_unsupported. An efa_test_… key reaches test inboxes only; an efa_live_… key cannot be aimed at them. Do not put a colleague’s real address in a fixture. Use example.org or another .invalid address for counterparties.
Inject inbound mail; do not wait for the internet
A .mail.invalid inbox cannot receive a real message, so you create one:
curl -X POST "https://api.emailforagents.ai/v1/projects/$PROJECT/test/inbound" \
-H "Authorization: Bearer $EFA_TEST_KEY" \
-H "Content-Type: application/json" \
-d '{
"inbox_id": "ibx_…",
"from": {"email": "buyer@example.org"},
"to": [{"email": "3f7c…@a91d….mail.invalid"}],
"subject": "Invoice 4821",
"text": "Could you resend the receipt?"
}'
This only works when both the project and the inbox are test — anything else is test_mode_required. It needs the test:simulate and messages:read scopes. The call creates a real thread and message through the same storage path live inbound mail uses, returns 201, and emits message.received with "environment": "test" and "simulated": true. Your event consumer and webhook signature checks run the production code path.
Simulated inbound mail is not counted against the test send limits. Each simulated message opens a new thread, so this endpoint will not, by itself, exercise a reply landing on an existing conversation. For the reply, pass reply_to_message_id set to the simulated message.
Send with a key that names the message, not the attempt
Every send — including a test send — takes an Idempotency-Key header, 1 to 200 characters. The rule in a sentence: the key names the message you meant to send, not the attempt you are making.
invoice-4821-receipt-v1 is a good key. You can reconstruct it after a process restart. A fresh UUID generated inside the retry loop is the worst possible key. It makes every attempt a distinct message.
curl -X POST "https://api.emailforagents.ai/v1/projects/$PROJECT/inboxes/$INBOX/messages" \
-H "Authorization: Bearer $EFA_TEST_KEY" \
-H "Idempotency-Key: invoice-4821-receipt-v1" \
-H "Content-Type: application/json" \
-d '{
"to": [{"email": "buyer@example.org"}],
"subject": "Re: Invoice 4821",
"reply_to_message_id": "msg_…",
"text": "Here is the receipt."
}'
A 202 can mean queued or approval_required. Check result, not only the status code. queued means accepted for sending, not delivered. If the inbox or the credential requires review, you get an approval_id and nothing has been sent.
Reusing a key has four outcomes worth asserting in CI:
| Situation | What you should see |
|---|---|
| Same key, same body | The original result replayed, including the same message_id. No second email. |
| Same key, different body |
idempotency_conflict. Fix the key derivation. Do not retry with a new key. |
| Same key, first attempt still in flight |
temporarily_unavailable. Back off, then re-read. |
| Same key, more than 30 days later |
idempotency_expired. The key stays reserved as a tombstone for 365 days so it cannot be silently recycled. |
A client that catches a conflict and retries with a fresh key has turned a caught bug into a duplicate email. Log it and stop.
One more unsafe retry: if a submission’s outcome is submission_unknown, do not send again under a new key. That could deliver twice. The message is held for reconciliation, not automatically retried.
What to assert before you touch a live inbox
A useful first suite, all against the test project:
-
list_inboxesreturns only the test inbox, with a.mail.invalidaddress andenvironment: "test". - A simulated inbound message appears as a thread, and
message.receivedcarriessimulated: true. - A draft-only key produces
approval_required, notqueued. - Approving the held draft (as a human, with its own
Idempotency-Key) moves the message throughqueuedtoacceptedon the simulated transport — and a second approve with the same key does not create a second message. - Repeating the send with the same
Idempotency-Keyand the same body returns the originalmessage_id. - Repeating it with a changed body returns
idempotency_conflict.
The allowances are small and enforced: up to three test inboxes per workspace, 100 sent test messages for the life of the workspace, and 10 MiB of test content. Those counts do not go back down, and test mail is not charged. If you hit the message cap mid-suite, the suite is probably accumulating fixtures it should be cleaning up.
When the loop is boringly reliable, create a separate live project, use an efa_live_… key, and grant the least access the job needs. Prefer draft-plus-approval over send for the first live week. The free live allowance is one inbox and 100 email units a month; a retry with the same idempotency key is not charged twice. There is no published SDK today, so the examples stay on plain HTTP. For limits and retry rules, see the API basics. Run the first mail in test mode, then give the agent a routable address.
Aljay Miskino is VP of Partnerships at Email for Agents (emailforagents.ai).
Top comments (0)