Email

Send, read, search, reply, archive, and draft emails β€” auto-detects AgentMail or SMTP/IMAP backend

Give your agents email superpowers. Just set env vars β€” tools auto-detect the right backend.

python
from praisonaiagents import Agent
from praisonai_tools import EmailTool

agent = Agent(name="InboxAssistant", tools=[EmailTool()])
agent.start("Check my inbox for unread messages from Bob")

The user asks about mail; the agent reads or sends email through the configured inbox tool.

Diagram

Auto-Detect Backend

AGENTMAIL_API_KEY

EMAIL_ADDRESS

πŸ“§ send_email
list_emails
search_emails
archive_email
…

πŸ” Env Vars?

⚑ AgentMail API

πŸ“¬ SMTP / IMAP

Quick Start

  1. Simple Usage
    python
    from praisonaiagents import Agent
    from praisonaiagents.tools.email_tools import send_email, list_emails, search_emails, archive_email
    
    agent = Agent(
        name="EmailAgent",
        instructions="You help send and manage emails.",
        tools=[send_email, list_emails, search_emails, archive_email]
    )
    
    agent.start("Check my inbox for unread messages from Bob")

    Set either backend via env vars β€” same code works for both:

    AgentMail (API)

    bash
    export AGENTMAIL_API_KEY="am_..."
    export AGENTMAIL_INBOX_ID="you@agentmail.to"

    Gmail / Outlook (SMTP)

    bash
    export EMAIL_ADDRESS="you@gmail.com"
    export EMAIL_PASSWORD="xxxx xxxx xxxx xxxx"  # App Password

  2. With Configuration

    Use the same tool with an agent β€” see Usage with Agent below, or pass env vars and options from the sections above.

Available Tools

Auto-Detect Tools (Recommended)

These work with both backends. Just set env vars and go.

ToolSignatureAgentMailSMTP/IMAP
send_email(to, subject, body)βœ… APIβœ… SMTP
list_emails(limit=10)βœ… APIβœ… IMAP
read_email(message_id)βœ… APIβœ… IMAP
reply_email(message_id, body)βœ… APIβœ… SMTP (In-Reply-To)
search_emails(query, from_addr, subject, label, after_date, before_date, limit)βœ… label/dateβœ… text/from/subject
archive_email(message_id)βœ… label toggleβœ… Gmailβ†’All Mail
draft_email(to, subject, body)βœ… drafts APIβœ… IMAP APPEND
forward_email(message_id, to, note)βœ… API❌ (graceful fallback)
send_draft(draft_id)βœ… drafts API❌ (graceful fallback)

AgentMail-Only Tools

ToolSignatureDescription
list_inboxes()List all inboxes for this API key
create_inbox(display_name)Create a new inbox

Using Tool Profiles

python
from praisonaiagents import Agent
from praisonaiagents.tools import resolve_profiles

# AgentMail tools (send, list, read, reply)
agent = Agent(tools=resolve_profiles("email"))

# SMTP/IMAP tools (backward-compat aliases)
agent = Agent(tools=resolve_profiles("smtp_email"))

Backward-Compatible Aliases

These still work but the auto-detect tools above are preferred:

AliasMaps To
smtp_send_email_smtp_send_email (SMTP only)
smtp_read_inbox_smtp_list_emails (IMAP only)
smtp_search_inbox_smtp_search_emails (IMAP only)
smtp_archive_email_smtp_archive_email (IMAP only)
smtp_draft_email_smtp_draft_email (IMAP only)

Environment Variables

VariableBackendDefaultDescription
AGENTMAIL_API_KEYAgentMailβ€”API key from agentmail.to
AGENTMAIL_INBOX_IDAgentMailβ€”Inbox email address
EMAIL_ADDRESSSMTP/IMAPβ€”Your email address
EMAIL_PASSWORDSMTP/IMAPβ€”App Password (recommended)
EMAIL_SMTP_SERVERSMTP/IMAPAuto-detectedSMTP server hostname
EMAIL_IMAP_SERVERSMTP/IMAPAuto-detectedIMAP server hostname

Gmail Setup

  1. Enable 2-Factor Authentication
  2. Generate App Password at Google Account
  3. Use App Password as EMAIL_PASSWORD

How It Works

Diagram
AgentMail / SMTPEmail ToolsAgentUserAgentMail / SMTPEmail ToolsAgentUser“Check my inbox”list_emails()Fetch messagesMessagesParsed inboxSummary

Best Practices

Use App Passwords, not your login

For Gmail and Outlook, generate an App Password and set it as EMAIL_PASSWORD. Never store your real account password.

Let auto-detect pick the backend

Set the env vars for one backend and use the auto-detect tools β€” the same agent code works for AgentMail or SMTP/IMAP.

Scope inbox access

Give the agent only the email tools it needs. A read-only assistant should not include send_email.

Filter searches

Use search_emails with from_addr, subject, or date filters so the agent processes a focused set of messages.


Related