Build Email Agents with Gemini's Function Calling

Connect Google Gemini's function calling to MultiMail for sending, reading, and managing email — with human oversight layered on top of Gemini's safety settings.


Google's Gemini API provides function calling and grounding capabilities that make it well-suited for building agentic applications. MultiMail gives Gemini models a complete email infrastructure layer, enabling your agents to send, receive, read, and reply to emails through structured function calls.

By integrating MultiMail with the Gemini API, your agents gain email capabilities while benefiting from two layers of safety: Gemini's built-in safety settings handle content filtering, and MultiMail's oversight modes handle email-specific authorization. The default gated_send mode means Gemini drafts emails but a human approves before delivery.

Connect Gemini to MultiMail by defining email functions using Gemini's function declaration format and routing calls to the MultiMail REST API. Gemini also supports automatic function calling, which can streamline your agent loop.

Built for Google Gemini API developers

Dual Safety Layers

Gemini's built-in safety settings filter harmful content at the model level. MultiMail adds email-specific oversight on top, so you get content safety from Google and send authorization from MultiMail's oversight modes.

Function Calling Integration

Gemini's function calling accepts structured declarations that map directly to MultiMail's email operations. Define send_email, check_inbox, and reply_email as function declarations with typed parameters.

Automatic Function Calling

Gemini supports automatic function calling where the SDK handles the tool execution loop. Combined with MultiMail's API, this simplifies building email agents to just defining functions and connecting endpoints.

Multimodal Email Processing

Gemini's multimodal capabilities let your agent process email attachments — images, PDFs, and documents — alongside text content. MultiMail handles the email transport while Gemini analyzes the content.

Graduated Trust via Oversight Modes

Start with gated_send (Gemini composes, human approves) and progress to monitored or autonomous as trust builds. MultiMail supports five oversight modes to match agent autonomy to your confidence level.


Get started in minutes

Define MultiMail Functions for Gemini
python
import google.generativeai as genai
import requests

genai.configure(api_key="your_gemini_api_key")
MULTIMAIL_API = "https://api.multimail.dev/v1"
MM_HEADERS = {"Authorization": "Bearer mm_live_your_api_key"}

send_email_fn = genai.protos.FunctionDeclaration(
    name="send_email",
    description="Send an email through MultiMail. In gated_send mode, queues for human approval.",
    parameters=genai.protos.Schema(
        type=genai.protos.Type.OBJECT,
        properties={
            "mailbox_id": genai.protos.Schema(type=genai.protos.Type.STRING),
            "to": genai.protos.Schema(type=genai.protos.Type.STRING),
            "subject": genai.protos.Schema(type=genai.protos.Type.STRING),
            "body": genai.protos.Schema(type=genai.protos.Type.STRING)
        },
        required=["mailbox_id", "to", "subject", "body"]
    )
)

check_inbox_fn = genai.protos.FunctionDeclaration(
    name="check_inbox",
    description="Check the inbox for recent messages.",
    parameters=genai.protos.Schema(
        type=genai.protos.Type.OBJECT,
        properties={
            "mailbox_id": genai.protos.Schema(type=genai.protos.Type.STRING),
            "limit": genai.protos.Schema(type=genai.protos.Type.INTEGER)
        },
        required=["mailbox_id"]
    )
)

email_tools = genai.protos.Tool(function_declarations=[send_email_fn, check_inbox_fn])

Create function declarations for email tools using Gemini's native function calling format.

Build an Email Agent with Chat Sessions
python
def execute_function(name, args):
    if name == "send_email":
        resp = requests.post(f"{MULTIMAIL_API}/send", headers=MM_HEADERS, json=dict(args))
    elif name == "check_inbox":
        resp = requests.get(
            f"{MULTIMAIL_API}/mailboxes/{args[&"cm">#039;mailbox_id']}/inbox",
            headers=MM_HEADERS, params={"limit": args.get("limit", 10)}
        )
    elif name == "reply_email":
        resp = requests.post(f"{MULTIMAIL_API}/reply", headers=MM_HEADERS, json=dict(args))
    else:
        return {"error": f"Unknown function: {name}"}
    return resp.json()

model = genai.GenerativeModel(
    model_name="gemini-2.0-flash",
    tools=[email_tools],
    system_instruction="You are an email assistant. Emails are sent in gated_send "
    "mode and queue for human approval before delivery."
)

chat = model.start_chat()
response = chat.send_message("Check my inbox and summarize what&"cm">#039;s new")

# Handle function calls
while response.candidates[0].content.parts[0].function_call:
    fc = response.candidates[0].content.parts[0].function_call
    result = execute_function(fc.name, fc.args)
    response = chat.send_message(
        genai.protos.Content(parts=[genai.protos.Part(
            function_response=genai.protos.FunctionResponse(
                name=fc.name, response={"result": result}
            )
        )])
    )

print(response.text)

Create a Gemini chat session with email tools and handle function calls against MultiMail.

Automatic Function Calling Mode
python
"cm"># Define callable functions that Gemini can invoke automatically
def send_email(mailbox_id: str, to: str, subject: str, body: str) -> dict:
    """Send an email through MultiMail. In gated_send mode, queues for approval."""
    resp = requests.post(f"{MULTIMAIL_API}/send", headers=MM_HEADERS, json={
        "mailbox_id": mailbox_id, "to": to, "subject": subject, "body": body
    })
    return resp.json()

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

"cm"># Enable automatic function calling
model = genai.GenerativeModel(
    model_name="gemini-2.0-flash",
    tools=[send_email, check_inbox]
)

"cm"># The SDK automatically executes function calls and feeds results back
response = model.generate_content(
    "Check inbox mbx_abc123 and draft a reply to the latest message",
    tool_config={"function_calling_config": {"mode": "AUTO"}}
)
print(response.text)

Use Gemini's automatic function calling to simplify the agent loop — the SDK handles tool execution.


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 the Google Generative AI SDK and requests library for calling the MultiMail API.

bash
pip install google-generativeai requests
3

Define Email Function Declarations

Create FunctionDeclaration objects for send_email, check_inbox, and other MultiMail operations using Gemini's typed schema format.

4

Create a Chat Session with Tools

Initialize a GenerativeModel with your email tools and start a chat session. Send messages and handle function call responses in a loop.

bash
model = genai.GenerativeModel(model_name="gemini-2.0-flash", tools=[email_tools])
chat = model.start_chat()
5

Approve Pending Emails

If your mailbox uses gated_send mode (the default), review and approve pending emails in the MultiMail dashboard before they are delivered.


Common questions

Can I use Gemini's automatic function calling with MultiMail?
Yes. Gemini's automatic function calling mode lets the SDK handle the tool execution loop. Define your MultiMail functions as Python callables, and the SDK will automatically invoke them when Gemini requests a function call. This simplifies your agent code significantly.
How do Gemini's safety settings interact with MultiMail's oversight?
They work as complementary layers. Gemini's safety settings filter harmful content at the model level before function calls are generated. MultiMail's oversight modes control whether emails are actually sent — gated_send queues them for human approval regardless of content safety scores.
What happens when Gemini sends an email in gated_send mode?
In gated_send mode, the MultiMail API returns a success response with a pending status. The email is queued for human review in the MultiMail dashboard. Once approved, it is delivered. Your agent can check the status of pending emails using the list_pending endpoint.
Can Gemini process email attachments?
Yes. Gemini's multimodal capabilities let it analyze images, PDFs, and documents. You can use MultiMail's download_attachment endpoint to retrieve attachments, then pass them to Gemini for analysis. For sending attachments, include base64-encoded content in the send_email call.
Is there rate limiting on the MultiMail API?
Rate limits depend on your plan tier. The Starter (free) plan allows 200 emails per month, while paid plans range from 5,000 to 150,000. The API returns standard 429 responses when limits are reached, which your agent can handle with retry logic.

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.