This guide will walk you through setting up the Gmail Email Sorter application from installation to execution.
- Python 3.8 or higher
- A Gmail account
- Internet connection
Download the code to your local machine.
# Install LangChain and LangGraph
pip install langchain langchain-core langgraph
# Install HuggingFace Transformers
pip install transformers torch
# Install Google API libraries
pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib- Go to the Google Cloud Console
- Click on the project dropdown at the top of the page
- Click "New Project"
- Enter a project name (e.g., "Email Sorter") and click "Create"
- Wait for the project to be created, then select it from the dropdown
- In the Google Cloud Console, navigate to "APIs & Services" > "Library" in the left sidebar
- Search for "Gmail API" and click on it
- Click the "Enable" button
- In the Google Cloud Console, navigate to "APIs & Services" > "OAuth consent screen"
- Select "External" user type (unless you have a Google Workspace account)
- Fill in the required information:
- App name: "Email Sorter" (or your preferred name)
- User support email: Your email address
- Developer contact information: Your email address
- Click "Save and Continue"
- On the "Scopes" page, click "Add or Remove Scopes"
- In the filter box, enter:
https://www.googleapis.com/auth/gmail.modify - Check the box next to this scope when it appears
- Click "Update" and then "Save and Continue"
- On the "Test users" page, click "Add Users"
- Add your Gmail address (the one you'll use with the sorter)
- Click "Save and Continue"
- Review your app information and click "Back to Dashboard"
- Note: Your app will be in "Testing" mode, which is fine for personal use
- In the Google Cloud Console, navigate to "APIs & Services" > "Credentials"
- Click "Create Credentials" > "OAuth client ID"
- Select "Desktop app" as the application type
- Name: "Email Sorter Client" (or your preferred name)
- Click "Create"
- You'll see a popup with your client ID and client secret
- Click "Download JSON" to download your credentials
- Rename the downloaded file to
credentials.json - Move this file to the same directory as your script
If you need to download your credentials again later:
- Go to Google Cloud Console > "APIs & Services" > "Credentials"
- Under "OAuth 2.0 Client IDs", find your client
- Click the download icon (↓) on the right side of your client's row
- Rename the downloaded file to
credentials.json - Replace your existing file with this new download
The script needs certain directories to store temporary files and sorted emails. Create these directories or ensure your code has the correct paths defined:
# Example directory structure
TEMP_PATH = "temp"
BASE_SAVE_PATH = "sorted_emails"Ensure your KEYWORD_RULES dictionary is properly defined in your code to match your email sorting needs:
# Example keyword rules
KEYWORD_RULES = {
"GitHub": ["STAR", "PR", "ISSUE"],
"Document": ["REPORT", "INVOICE", "CONTRACT"],
# Add more rules as needed
}- Run the script:
python email_sorter.py - A browser window will open asking you to log in to your Google account
- Select your account and click "Continue"
- Review the permissions and click "Continue"
- The browser will show "The authentication flow has completed."
- Return to your terminal. The script should now be running and monitoring for new emails.
After the first authentication, the script will:
- Skip all existing emails in your inbox
- Wait for new emails to arrive
- Process each new email through your sorting pipeline
- Sort emails into the appropriate folders based on your rules
To stop the script, press Ctrl+C in your terminal.
-
Error: "Client secrets not found"
Make sure yourcredentials.jsonfile is in the same directory as your script. -
Error: "Access denied"
Make sure you've added your email as a test user in the OAuth consent screen. -
Error: "This app isn't verified"
When authenticating, you might see a warning that the app isn't verified. Click "Advanced" and then "Go to [Your App Name] (unsafe)" to proceed. This is normal for apps in testing mode. -
Error: "redirect_uri_mismatch"
Ensure you're using the correct credentials.json file. Delete any token.pickle file and try again.
-
Error: "Gmail API not enabled"
Make sure you've enabled the Gmail API in your Google Cloud project. -
Error: "Invalid grant"
If your token expires, delete thetoken.picklefile and run the script again to re-authenticate. -
Error: "Daily Limit Exceeded" or "User Rate Limit Exceeded"
You may be making too many requests to the Gmail API. Try increasing the polling interval.
- If emails aren't being sorted correctly, check your
KEYWORD_RULESand the logic in your processing functions. - Check the console output for specific error messages related to the LLM or sorting process.
- Reset authentication: Delete the
token.picklefile to force re-authentication - Check API quotas: If you're processing many emails, you might hit API quotas. Check the Google Cloud Console for quota usage.
- Debug logs: Add more print statements to your code to track where errors occur