Skip to main content

mailsai

Email API for AI agents — Python SDK.

Per-agent email addresses on your domain, per-agent reputation, and prompt-injection scanning on every inbound. Your agent reads the reply and decides what to say — intent + entity extraction is opt-in.

pip install mailsai

Quick start

Create the agent once (this mints its inbox address), then send from it. Skipping the create step is the most common first-run error — agent("sarah") only references an agent, it does not create one, so sending returns 404 agent_not_found.

from mailsai import Client, agent

# Reads MAILS_API_KEY from env (or pass api_key=... explicitly)
client = Client()

# One-time setup: create the agent. It gets sarah@<your-workspace>.mails.ai
client.create_agent("sarah")

sarah = agent("sarah")

# mails.ai is transactional-only: mail the recipient asked for. Cold outreach and bulk
# marketing are refused with 422 cold_email_prohibited — that is deliberate, and it is
# what keeps the sending reputation clean for everyone on the platform.
sarah.send(
    to="you@example.com",
    subject="Your verification code is 481902",
    body="Your verification code is 481902. It expires in 15 minutes.",
)

@sarah.on_reply
def handle(reply):
    # Always present — the delivery + security + identity layer:
    # reply["injection_score"]   -> 0.02   # six-category prompt-injection scan
    # reply["sender_reputation"] -> 0.91   # per-agent reputation
    #
    # Present only when classification is enabled (opt-in, +$0.003/inbound):
    # reply["intent"]            -> "schedule_demo" | "ask_question" | …
    # reply["entities"]          -> {"date": "...", "time": "..."}
    # reply["urgency"]           -> 0.8
    #
    # Your agent reads the reply and decides what to send next.
    print(reply["injection_score"], reply["sender_reputation"])

sarah.start_listening()  # blocks; opens SSE stream and dispatches replies

For non-blocking, pass blocking=False to start_listening() — it returns a daemon thread.

Lower-level client

from mailsai import create_client

c = create_client()  # reads MAILS_API_KEY

# Send via any agent
c.send("sarah", to="lead@example.com", subject="Demo", body_text="…")

# Resources
threads = c.list_threads(agent_id="agent_abc123")
usage = c.usage()
rep = c.get_reputation(agent_id="agent_abc123")

# Drafts
draft = c.create_draft(agent="sarah", to="lead@example.com", subject="Demo", body_text="…")
c.send_draft(draft["id"])

Webhook verification

from mailsai import verify_webhook

# In your webhook handler:
event = verify_webhook(
    body=request.body.decode(),
    signature=request.headers["X-Mails-Signature"],
    secret=os.environ["MAILS_WEBHOOK_SECRET"],
)
if event is None:
    return Response(status=400)
# event is the verified inbound event dict

Errors

from mailsai import MailsError

try:
    sarah.send(to="blocked@example.com", subject="…", body="…")
except MailsError as e:
    print(e.type, e.code, e.message, e.status, e.request_id)

Configuration

Param / env var Default
api_key / MAILS_API_KEY (required)
base_url / MAILS_BASE_URL https://api.mails.ai

Documentation

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mailsai-0.2.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mailsai-0.2.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file mailsai-0.2.1.tar.gz.

File metadata

  • Download URL: mailsai-0.2.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.5

File hashes

Hashes for mailsai-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1f94e7228ccb1c9ec5be5990025d7e38e2632afb94fe565c3e85c876c5f05907
MD5 8cc04dec690f862384f7d2fc33070a14
BLAKE2b-256 eb8695bd20bdeecd7c620a1fb8c078141b0b92d001dd0788a96cdbb5509e0522

See more details on using hashes here.

File details

Details for the file mailsai-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: mailsai-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.5

File hashes

Hashes for mailsai-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 54c69984bb5a5483faa09564f1e93624bce600e731225cec9fdb5425b3d94f2d
MD5 ded0988a51dcd2db81c31f164db5385b
BLAKE2b-256 632c300b9a6a047c0926b48600aa9c76236a8259595bb4d14fece352c77eeb0a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page