Newsletter Agent
Creating an agent that monitors incoming emails and sends most relevant emails to user
Creating a Newsletter Agent with AgentMail
This is an example implementation of how Agent’s can be equipped with their own inbox and interact with internet resources autonomously, and do the heavy duty lifting of tedious tasks.
Here we will be creating an agent that monitors incoming emails(to its own inbox of course) and sends the most relevant emails regarding newsletter news tailored to the user’s preferences straight to their inbox.
Here is what the agent will be capable of:
- Monitor an inbox for new messages
- Process newsletter signup requests
- Manage promotional content
- Send relevant updates to users
Quick Start
This tutorial makes use of the library Browser-use, which requires usage of Python 3.11 or higher. If you want to create an agent in Node please follow the Node.js Tutorial coming soon
Install Dependencies
While using the SDK is recommended, if you prefer making direct HTTP requests instead of using the SDK, you’ll only need:
Creating the Websocket Connection
It seems counterintuitive to have an email sitting in the inbox and just have the agent read the email sitting in the inbox and react to it.
So what we will do is create a websocket connection to the AgentMail inbox, and have it listen for incoming emails. Once the email regarding the coupon code is received, the agent will then be spun up to perform the task outlined.
Lets create a connect file and start by importing the necessary libraries.
Lets create a function that will connect to the AgentMail inbox and listen for incoming emails.
Websocket is hosted locally, but if you plan on deploying this to production, you will need to host on your own server/domain.
We created the Agentmail client instance, and can interact with the service via SDK calls.
If you prefer making direct HTTP requests instead of using the SDK:
Make sure to handle rate limits appropriately. The 5-second interval might need adjustment based on your use case and API limits.
What this code does is it creates a websocket connection to the AgentMail inbox and constatly checks for new emails in 5 second intervals via the get_emails() function.
Once the email is received, it sends a notification through the websocket to our own server to spin the agent up to perform the task outlined.
Creating the Agent
Lets get the imports out of the way first.
Browser
We will be using the Browser-use library to create a browser instance and give our agent access to our browser so it can browse the web and sign up for the coupon codes.
The implementation of the browser-use library is a bit complex, but the basic idea is that we are creating a browser instance and then passing it to the agent and for the sake of this demo we will give it the following configurations:
- Headless mode is set to false, so the browser will be visible to the user.
- Disable security is set to true, so the browser will not be secure.
- Chrome instance path is set to the path of the chrome browser on your machine.
- New context config is set to the following:
You can play around with the configurations and see what works best for you. Here is the browser-use documentation for more information.
Lets load the .env
file and create the browser instance.
Lets create the instance that will manage the email inbox and websocket connection.
The task is an f string that will be passed to the agent as instructions. Here is the task that we will be passing:
Thats quite lengthy, but it ensures accuracy and the agent will not deviate from the task.
With that out of the way, we will create the following:
- Browser Instance (to browse the web)
- Controller Instance (browser-use way of defining tools to give to the agent)
- App Instance (to host the websocket connection)
- AgenetMail Instance (to interact with the inbox)
Tool Calls
Here is the fun part. Tool calls.
We will be creating tool calls for all the necessary endpoints, but feel free to play around with the code and create your own.
Browser-use has a specific way to define custom functions(tools) that will be passed to the agent. This is in the form of controller.action(). It effectively is the @tool
decorator in langchain.
These are some elementary tool calls we defined ourselves. Please feel free to play around with the code and create your own given the methods available in the AgentMail SDK.
Finally, lets define the websocket endpoint that will be used to receive the notification a new email just came in, so we can spin up the agent to perform the task outlined.
We communicate via these json messages, but feel free to play around with what information is sent over the websocket.
Finally, lets run the app:
BOOM!
You’re done. Open up 2 terminals and run the following:
Running agent.py will open the websocket connection via uvicorn and fast API. Connect.py will connect to this websocket connection and listen for new emails.
Once it receives one it will send a notification to the agent.py file, which will spin up the agent and perform the task outlined.
To see everything happen in real time, open up the browser and go to the inbox of the email you are monitoring on the AgentMail demo site
Feel free to play around with the code and see what you can come up with!
You can find here the full code for the Shopper Agent