Loading...
Loading...
Comprehensive guide to why and how AI agents should use email. Use when evaluating whether an agent needs email, comparing email infrastructure options (AgentMail vs Gmail API vs Resend vs SendGrid vs SES), understanding security risks like prompt injection via email and OAuth credential exposure, or exploring common agent email use cases such as customer support agents, sales outreach, verification flows, and browser automation.
npx skill4agent add agentmail-to/agentmail-skills email-for-ai-agentsfrom agentmail import AgentMail, Subscribe, MessageReceivedEvent
from agentmail.inboxes.types import CreateInboxRequest
client = AgentMail()
inbox = client.inboxes.create(
request=CreateInboxRequest(username="support", client_id="support-v1"),
)
with client.websockets.connect() as socket:
socket.send_subscribe(Subscribe(inbox_ids=[inbox.inbox_id]))
for event in socket:
if isinstance(event, MessageReceivedEvent):
msg = event.message
reply_text = msg.extracted_text or msg.text
# Classify, generate response, send or draftfrom agentmail import AgentMail
from agentmail.inboxes.types import CreateInboxRequest
client = AgentMail()
outbox = client.inboxes.create(
request=CreateInboxRequest(username="sales", client_id="sales-v1"),
)
prospects = [{"email": "jane@acme.com", "name": "Jane", "company": "Acme"}]
def generate_personalized_email(prospect: dict) -> str:
# Your LLM-backed copywriting goes here.
return f"Hi {prospect['name']}, ..."
for prospect in prospects:
client.inboxes.messages.send(
outbox.inbox_id,
to=prospect["email"],
subject=f"Quick question about {prospect['company']}",
text=generate_personalized_email(prospect),
labels=["outreach", "sequence-1"],
)import re
signup_inbox = client.inboxes.create()
# Use signup_inbox.email to register on a website
# Wait for OTP
with client.websockets.connect() as socket:
socket.send_subscribe(Subscribe(inbox_ids=[signup_inbox.inbox_id]))
for event in socket:
if isinstance(event, MessageReceivedEvent):
match = re.search(r"\b(\d{4,8})\b", event.message.text or "")
if match:
otp_code = match.group(1)
breakagent-email-patternsreferences/infrastructure-comparison.md| Need | Best choice | Why |
|---|---|---|
| Agent needs its own inbox | AgentMail | Instant inbox creation, two-way conversations, WebSocket support |
| Two-way email conversations | AgentMail | Native thread management, extracted_text for reply parsing |
| Send-only notifications | Resend or SendGrid | Optimized for transactional sending |
| Read a human's Gmail | Gmail API | Direct access to existing mailbox (with security caveats) |
| High-volume marketing | SendGrid or Mailgun | Built for bulk sending with deliverability tools |
| AWS-native infrastructure | Amazon SES | Cheapest at scale, integrates with Lambda/SNS |
references/security-risks.mdpip install agentmail # Python
npm install agentmail # TypeScriptfrom agentmail import AgentMail
client = AgentMail() # reads AGENTMAIL_API_KEY from env
inbox = client.inboxes.create()
client.inboxes.messages.send(
inbox.inbox_id,
to="user@example.com",
subject="Hello from my agent",
text="This agent has its own email address!",
)agentmailagent-email-patternsreferences/infrastructure-comparison.mdreferences/security-risks.md