This project provides an automated email processing system that integrates with Gmail and uses OpenAI's GPT-3.5-turbo model to analyze and respond to emails. It uses Express for the server, Google APIs for Gmail interactions, node-cron for scheduling tasks, and BullMQ for job queue management.
- OAuth2 Authentication: Allows users to authenticate via Google OAuth2.
- Email Fetching: Retrieves unread emails from Gmail.
- Email Replying: Automatically replies to emails based on their content.
- Email Labeling: Labels emails based on content analysis.
- Scheduling: Checks and processes emails at regular intervals.
- Queue Management: Uses BullMQ for handling email processing tasks asynchronously.
- Node.js
- Typescript
- npm or yarn
- Redis server(for BullMQ)
-
Clone the repository:
git clone https://github.com/ayushgupta4002/MailR.git cd MailR
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory and add the following environment variables:CLIENT_ID=<your-google-client-id> CLIENT_SECRET=<your-google-client-secret> OPENAI_API_KEY=<your-openai-api-key>
-
Start Redis server (if not already running):
docker run -it -p 6379:6379 -d redis
-
Start the Express server:
npm run dev
-
Authenticate with Google:
- Open a browser and go to
http://localhost:5000/auth
. - Complete the authentication process and allow access to your Gmail account.
- Open a browser and go to
Initiates the Google OAuth2 authentication process. Redirects the user to the Google login page.
2. GET /oauth2callback/google-callback
[This is a redirect route,You Don't need to visit this route manually]
Callback endpoint for Google OAuth2. Handles the OAuth2 response, stores the tokens, and starts the cron job for checking emails.
Fetches and returns a list of unread emails from the authenticated Gmail account.
Retrieves the authenticated user's information (email and id) from Google.
Lists unread messages from the user's Gmail account.
Gets detailed information about a specific message.
Creates a new Gmail label if it doesn't already exist.
Generates a base64-encoded email reply.
Checks for unread emails and processes them by adding tasks to the BullMQ queue.
Processes jobs added to the processQueue
. Each job involves sending a reply to an email and labeling it based on content analysis using OpenAI's GPT-3.5-turbo model.
-
Start the application:
npm run dev
-
Authenticate with Google by visiting
http://localhost:5000/auth
. -
Check emails by visiting
http://localhost:5000/emails
. -
Wait for cron job to automatically check and process emails every minute.
This project provides a robust system for automating email responses and labeling using AI. It integrates seamlessly with Gmail and leverages the power of OpenAI's GPT-3.5-turbo model to understand and respond to email content professionally.