Email for AG2 Multi-Agent Conversations

Give AG2 conversable agents email capabilities with MultiMail — centralized oversight ensures every email from any agent in the group is properly authorized.


AG2 is a community fork of AutoGen that provides a multi-agent conversation framework. It maintains API compatibility with AutoGen while adding community-driven features for production use. When AG2 agents in a group chat need to send email, MultiMail provides centralized oversight that catches sends from any agent in the conversation.

In AG2's multi-agent setup, multiple agents may independently decide to trigger email actions. MultiMail's centralized oversight is essential here: regardless of which agent in the group chat calls the send endpoint, the email is gated for human review in gated_send mode.

Integration uses AG2's function registration system. Define email functions and register them with your AssistantAgent instances, just as you would with AutoGen.

Built for AG2 developers

Centralized Oversight for Group Chats

AG2 group chats involve multiple agents that can all trigger actions. MultiMail's centralized oversight ensures every email from any agent passes through the same review queue, preventing unauthorized sends.

AutoGen API Compatibility

AG2 maintains AutoGen API compatibility, so MultiMail integrations written for AutoGen work with AG2 unchanged. Same function registration, same tool calling patterns, same email oversight.

Production-Ready Email Safety

AG2 focuses on production readiness. MultiMail's oversight modes align with this goal — graduated trust from gated_all to autonomous lets you deploy email-capable agents with appropriate safety controls.

Code Execution Monitoring

AG2 agents can generate and execute code that might include email operations. MultiMail's API-level oversight catches email sends from both registered functions and dynamically generated code.


Get started in minutes

Register Email Functions with AG2 Agents
python
import requests
from autogen import AssistantAgent, UserProxyAgent

MULTIMAIL_API = "https://api.multimail.dev/v1"
HEADERS = {"Authorization": "Bearer mm_live_your_api_key"}

def send_email(to: str, subject: str, body: str,
               mailbox_id: str) -> str:
    """Send an email via MultiMail.
    In gated_send mode, queues for human approval."""
    resp = requests.post(f"{MULTIMAIL_API}/send",
                         headers=HEADERS,
                         json={"mailbox_id": mailbox_id,
                               "to": to, "subject": subject,
                               "body": body})
    return str(resp.json())

def check_inbox(mailbox_id: str, limit: int = 10) -> str:
    """Check inbox for recent messages."""
    resp = requests.get(
        f"{MULTIMAIL_API}/mailboxes/{mailbox_id}/inbox",
        headers=HEADERS, params={"limit": limit})
    return str(resp.json())

def reply_email(message_id: str, body: str) -> str:
    """Reply to an email within its thread."""
    resp = requests.post(f"{MULTIMAIL_API}/reply",
                         headers=HEADERS,
                         json={"message_id": message_id,
                               "body": body})
    return str(resp.json())

Define email functions and register them with AG2's AssistantAgent for tool calling.

AG2 Email Agent Setup
python
from autogen import AssistantAgent, UserProxyAgent, config_list_from_json

llm_config = {
    "config_list": [{"model": "gpt-4o", "api_key": "your_openai_key"}],
    "temperature": 0.1
}

"cm"># Create the email assistant
email_assistant = AssistantAgent(
    name="EmailAssistant",
    system_message=(
        "You are an email assistant using MultiMail. "
        "Use the provided functions to send, read, and reply to "
        "emails. All outbound emails use gated_send mode — they "
        "are queued for human approval before delivery. "
        "Always confirm email content with the user before sending."
    ),
    llm_config=llm_config
)

"cm"># Create the user proxy (handles function execution)
user_proxy = UserProxyAgent(
    name="User",
    human_input_mode="NEVER",
    code_execution_config={"work_dir": "email_workspace"}
)

"cm"># Register email functions
for func in [send_email, check_inbox, reply_email]:
    email_assistant.register_for_llm(
        name=func.__name__,
        description=func.__doc__
    )(func)
    user_proxy.register_for_execution(
        name=func.__name__
    )(func)

"cm"># Start the conversation
user_proxy.initiate_chat(
    email_assistant,
    message="Check my inbox and summarize any new messages."
)

Configure an AG2 AssistantAgent with email tools and appropriate system message.

Group Chat with Email Coordination
python
from autogen import GroupChat, GroupChatManager

"cm"># Create specialized agents
researcher = AssistantAgent(
    name="Researcher",
    system_message="You research topics and provide summaries. "
                   "You do not send emails directly.",
    llm_config=llm_config
)

email_agent = AssistantAgent(
    name="EmailAgent",
    system_message=(
        "You handle email communications via MultiMail. "
        "Only send emails when explicitly asked or when the "
        "group agrees on the content. All sends use gated_send "
        "mode (queued for human approval)."
    ),
    llm_config=llm_config
)

"cm"># Register email functions only with the email agent
for func in [send_email, check_inbox, reply_email]:
    email_agent.register_for_llm(
        name=func.__name__, description=func.__doc__
    )(func)
    user_proxy.register_for_execution(
        name=func.__name__
    )(func)

"cm"># Create group chat
group_chat = GroupChat(
    agents=[user_proxy, researcher, email_agent],
    messages=[],
    max_round=10
)

manager = GroupChatManager(
    groupchat=group_chat, llm_config=llm_config
)

user_proxy.initiate_chat(
    manager,
    message="Research AI safety best practices and email "
            "a summary to [email protected]"
)

Set up an AG2 group chat where agents collaborate on email tasks with centralized oversight.


Step by step

1

Create a MultiMail Account and API Key

Sign up at multimail.dev, create a mailbox, and generate an API key from your dashboard. Your key will start with mm_live_.

2

Install Dependencies

Install AG2 and the HTTP library for calling the MultiMail API.

bash
pip install ag2 requests
3

Define and Register Email Functions

Create email functions that call the MultiMail API. Register them with your AssistantAgent using register_for_llm and with UserProxyAgent using register_for_execution.

4

Configure Your Agent or Group Chat

Set up an AssistantAgent with a system message explaining the oversight mode, or create a group chat with a designated email agent.

5

Approve Pending Emails

Review emails queued by AG2 agents in the MultiMail dashboard. Approve or reject before delivery.


Common questions

Is AG2 compatible with AutoGen MultiMail integrations?
Yes. AG2 maintains API compatibility with AutoGen. Email function registrations, tool calling patterns, and agent configurations written for AutoGen work with AG2 without modification. MultiMail API calls are identical in both frameworks.
How do I prevent multiple agents from sending duplicate emails?
Register email functions only with one designated email agent in the group chat. Other agents discuss and draft content, but only the email agent can actually invoke send_email. MultiMail's gated_send mode provides an additional safety layer where duplicates can be caught during review.
Can AG2 agents execute code that sends emails?
AG2's code execution feature lets agents run generated Python code. If this code calls the MultiMail API, the oversight is still enforced server-side. In gated_send mode, emails from generated code are queued for review just like emails from registered functions.
How does AG2 handle email function errors?
MultiMail returns standard HTTP error codes that your functions convert to strings for the agent. The agent sees the error message and can retry or modify its approach. Common errors include 429 (rate limited) and 400 (invalid parameters). The agent's LLM reasoning helps it adapt to these errors.

Explore more

The only agent email with a verifiable sender

Email infrastructure built for AI agents. Verifiable identity, graduated oversight, and a 38-tool MCP server. Formally verified in Lean 4.