How to Send Email from an AI Agent (Python + Node.js)
Send email from your AI agent in under five minutes. Code examples in Python and Node.js for basic sends, HTML email, threading, and a complete agent loop.

This tutorial shows how to send email from an AI agent using Robotomail's REST API. Code examples in both Python and Node.js. You'll go from zero to sending in under five minutes.
Prerequisites
You need a Robotomail account and API key. If you don't have one, your agent can create both in a single request:
curl -X POST https://api.robotomail.com/v1/signup \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "a-strong-password",
"slug": "myagent"
}'The response includes your API key and a default mailbox at [email protected]. Save the API key and mailbox ID.
Send a basic email (Python)
import requests
API_KEY = "rm_live_..."
MAILBOX_ID = "mbx_..."
BASE_URL = "https://api.robotomail.com/v1"
response = requests.post(
f"{BASE_URL}/mailboxes/{MAILBOX_ID}/messages",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
},
json={
"to": ["[email protected]"],
"subject": "Hello from my AI agent",
"bodyText": "This email was sent by an autonomous agent.",
},
)
message = response.json()
print(f"Sent: {message['id']}")Send a basic email (Node.js)
const API_KEY = "rm_live_...";
const MAILBOX_ID = "mbx_...";
const BASE_URL = "https://api.robotomail.com/v1";
const response = await fetch(
`${BASE_URL}/mailboxes/${MAILBOX_ID}/messages`,
{
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
to: ["[email protected]"],
subject: "Hello from my AI agent",
bodyText: "This email was sent by an autonomous agent.",
}),
}
);
const message = await response.json();
console.log(`Sent: ${message.id}`);Send HTML email
Add a bodyHtml field alongside or instead of bodyText. Providing both lets email clients fall back to plain text when HTML rendering isn't available.
response = requests.post(
f"{BASE_URL}/mailboxes/{MAILBOX_ID}/messages",
headers=headers,
json={
"to": ["[email protected]"],
"subject": "Weekly report",
"bodyText": "Your weekly metrics are attached.",
"bodyHtml": "<h1>Weekly Report</h1><p>Your metrics are attached.</p>",
},
)Reply to a thread
To reply to an existing conversation, include the inReplyTo field with the message ID you're responding to. Robotomail sets the correct In-Reply-To and References headers automatically, so the reply threads correctly in the recipient's email client.
# When your agent receives an inbound message via webhook,
# the payload includes the message ID. Use it to reply:
inbound_message_id = "msg_xyz789"
response = requests.post(
f"{BASE_URL}/mailboxes/{MAILBOX_ID}/messages",
headers=headers,
json={
"to": ["[email protected]"],
"subject": "Re: Hello from my AI agent",
"bodyText": "Thanks for your reply! Here's what I found...",
"inReplyTo": inbound_message_id,
},
)Complete agent loop
Here's a minimal Python agent that sends an email and handles replies via webhook. This pattern works with any LLM (Claude, GPT, Llama) for generating responses.
from flask import Flask, request
import requests
app = Flask(__name__)
@app.route("/webhooks/email", methods=["POST"])
def handle_inbound():
payload = request.json
message = payload["data"]
# Extract the inbound message details
from_addr = message["from"]
body = message["bodyText"]
message_id = message["id"]
# Generate a reply (plug in your LLM here)
reply_text = generate_reply(body)
# Send the reply, threaded to the original message
requests.post(
f"{BASE_URL}/mailboxes/{MAILBOX_ID}/messages",
headers=headers,
json={
"to": [from_addr],
"subject": f"Re: {message['subject']}",
"bodyText": reply_text,
"inReplyTo": message_id,
},
)
return "", 200For the full inbound setup (registering webhooks, verifying signatures), see how to receive inbound email via webhook.
Next steps
You now have the building blocks for an email-capable AI agent. From here: