Skip to content

SOP: Send & Reply to Email

Fresh 🌱

The core conversational loop: send an initial email, then reply within the same thread.

1. Send an email

Always provide both text and html for deliverability and client compatibility.

python
from agentmail import AgentMail
client = AgentMail()

msg = client.inboxes.messages.send(
    inbox_id="outreach@agentmail.to",
    to=["jane@example.com"],
    subject="Following up",
    text="Hi Jane,\n\nThis is the plain-text version.",
    html="<p>Hi Jane,</p><p>This is the <strong>HTML</strong> version.</p>",
    labels=["outreach-campaign"],
)
bash
curl -X POST https://api.agentmail.to/v0/inboxes/<inbox_id>/messages/send \
  -H "Authorization: Bearer $AGENTMAIL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "to": ["jane@example.com"], "subject": "Following up",
        "text": "Hi Jane, ...", "html": "<p>Hi Jane, ...</p>" }'

2. Find conversations that need a reply

Use labels to track state. List threads marked unreplied:

python
threads = client.threads.list(labels=["unreplied"])
if threads.count == 0:
    print("Nothing to reply to.")
else:
    thread = threads.threads[0]

3. Reply within the thread

Replying keeps the message in the same thread with correct In-Reply-To headers.

python
client.inboxes.messages.reply(
    inbox_id="outreach@agentmail.to",
    message_id=thread.messages[-1].message_id,
    text="Thanks for the reply, Jane!",
    html="<p>Thanks for the reply, Jane!</p>",
)
bash
curl -X POST https://api.agentmail.to/v0/inboxes/<inbox_id>/messages/<message_id>/reply \
  -H "Authorization: Bearer $AGENTMAIL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "text": "Thanks!", "html": "<p>Thanks!</p>" }'

Receiving in real time

Polling works, but Webhooks and WebSockets are the efficient way to react to inbound mail the moment it arrives.