Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Refactor email handling in 2FA example

Magnus Müller69fdace314f7
diff --git a/examples/integrations/agentmail/2fa.py b/examples/integrations/agentmail/2fa.py
index aa65df025..eda3b09be 100644
--- a/examples/integrations/agentmail/2fa.py
+++ b/examples/integrations/agentmail/2fa.py
@@ -1,7 +1,18 @@
import asyncio
+import os
+import sys
+
+from agentmail import AsyncAgentMail # type: ignore
+
+from browser_use import Agent, Browser, models
+from examples.integrations.agentmail.email_tools import EmailTools
+
+sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
+
+from dotenv import load_dotenv
+
+load_dotenv()
-from browser_use import Agent, ChatOpenAI
-from examples.integrations.agentmail.email_tools import EmailController
TASK = """
Go to reddit.com, create a new account (use the get_email_address), make up password and all other information, confirm the 2fa, and like latest post on r/elon subreddit.
@@ -9,9 +20,22 @@ Go to reddit.com, create a new account (use the get_email_address), make up pass
async def main():
- email_controller = EmailController()
- llm = ChatOpenAI(model='gpt-4.1-mini')
- agent = Agent(task=TASK, controller=email_controller, llm=llm)
+ # Create email inbox
+ # Get an API key from https://agentmail.to/
+ email_client = AsyncAgentMail()
+ inbox = await email_client.inboxes.create()
+ print(f'Your email address is: {inbox.inbox_id}\n\n')
+
+ # Initialize the tools for browser-use agent
+ tools = EmailTools(email_client=email_client, inbox=inbox)
+
+ # Initialize the LLM for browser-use agent
+ llm = models.openai_gpt_4_1_mini
+
+ # Set your local browser path
+ browser = Browser(executable_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome')
+
+ agent = Agent(task=TASK, tools=tools, llm=llm, browser=browser)
await agent.run()
diff --git a/examples/integrations/agentmail/email_tools.py b/examples/integrations/agentmail/email_tools.py
index 81c26788d..438525c6c 100644
--- a/examples/integrations/agentmail/email_tools.py
+++ b/examples/integrations/agentmail/email_tools.py
@@ -19,8 +19,13 @@ if not logging.getLogger().handlers:
logger = logging.getLogger(__name__)
-class EmailController(Tools):
- def __init__(self, email_client: AsyncAgentMail | None = None, email_timeout: int = 30):
+class EmailTools(Tools):
+ def __init__(
+ self,
+ email_client: AsyncAgentMail | None = None,
+ email_timeout: int = 30,
+ inbox: Inbox | None = None,
+ ):
super().__init__()
self.email_client = email_client or AsyncAgentMail()
@@ -28,7 +33,7 @@ class EmailController(Tools):
self.register_email_tools()
- self.inbox: Inbox | None = None
+ self.inbox: Inbox | None = inbox
def _serialize_message_for_llm(self, message: Message) -> str:
"""
@@ -75,12 +80,10 @@ class EmailController(Tools):
If you are not on free tier it is recommended to create 1 inbox per agent.
"""
- inboxes = await self.email_client.inboxes.list()
+ if self.inbox:
+ return self.inbox
- if not inboxes.inboxes:
- return await self.create_inbox_client()
-
- return inboxes.inboxes[0]
+ return await self.create_inbox_client()
async def create_inbox_client(self) -> Inbox:
"""
@@ -89,6 +92,7 @@ class EmailController(Tools):
If you are not on free tier it is recommended to create 1 inbox per agent.
"""
inbox = await self.email_client.inboxes.create()
+ self.inbox = inbox
return inbox
async def wait_for_message(self, inbox_id: InboxId) -> Message: